Ajk
Ajk

Reputation: 21

Atlas search on an ObjectId field doesn't work

I was trying to implement an Atlas search query on an ObjectId field but nothing is being matched. Here's the index definition.

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "hscode": {
        "foldDiacritics": true,
        "maxGrams": 4,
        "minGrams": 1,
        "tokenization": "edgeGram",
        "type": "autocomplete"
      },
      "title": {
        "foldDiacritics": true,
        "maxGrams": 10,
        "minGrams": 2,
        "tokenization": "edgeGram",
        "type": "autocomplete"
      },
      "type._id": {
        "type": "objectId"
      }
    }
  }
}

The query details are as follows

db.categories.aggregate([
    {
        $search:{
            index:"categorySearch",
            equals:{
                path:"type._id",
                value:ObjectId("61711b4275e9397ec79bdfed")
            }
        }
    }
])

Note : The autocomplete queries are working without a problem. Thanking you in advance.

Upvotes: 1

Views: 792

Answers (1)

Ajk
Ajk

Reputation: 21

For those looking for a fix, try changing the index to look like this. The document type should be added for it to work.

type:{
  _id:{
    type:"objectId"
  }
  type:"document"
}

Upvotes: 1

Related Questions