How to perform MongoDB Atlas facets for ObjectId

As you know, we have 3 types of facets: string, number, and date facets.

Well, I want my facest for 2 fields in my document, src_n which is a string, and src_oid which is ObjectId.

How can I apply a facet since it is an object, not a number or string? Any idea to deal with it, please?

results_2 = list(
    my_collection.aggregate(
        [
            {
                "$searchMeta": {
                    "index": "TextIndex",
                    "facet": {
                        "facets": {
                            "src_n": {
                                "type": "string",
                                "path": "src_n",
                                "numBuckets": 1000,
                            },
                            "src_oid": {
                                "type": ObjectId,
                                "path": "src_oid",
                                "numBuckets": 1000,
                            },
                        },
                    },
                },
            },
        ]
    )
)

Hi @ahmad_al_sharbaji , we currently do not support faceting on ObjectIds. You can vote on this feature request here.

As a workaround, you can transform the src_oid into a string type and include it in your search index as a stringFacet.

1 Like

Thank you for your response!
Considering the size of our database, which is approximately 55 million documents, implementing that suggestion may not be ideal. However, I appreciate your link and have voted for it.

If you have any other suggestions, please feel free to let me know.

Best regards.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.