Atlas search filter documents with objectId

How can we filter the documents with objectId in with Atlas search filter?

The simplest way is to use the equals operator.

{
  index: '<index-name>',
  compound :{
    filter:[{
      equals: {
        query: someObjectID,
        path: "<path-of-object-id>"
      }
    }
    ]
  }
}
2 Likes

so when we have a loot of object id’s in that case we will have to crate this equals query with loop but then it’s again not a good solution.

it would be great if we can use it like this

"filter": [{
      "text": {
        "query": [ objectid1, objectid2, ... ],
        "path": "role"
      }
    }]

Not quite in the format that you wish but this example is taken from the link shared by @Marcus.

db.users.aggregate([
  {
    "$search": {
      "compound": {
        "should": [
          {
            "equals": {
              "path": "teammates",
              "value": ObjectId("5a9427648b0beebeb69537a5")
            }
          },
          {
            "equals": {
              "path": "teammates",
              "value": ObjectId("59b99dbdcfa9a34dcd7881d1")
            }
          },
          {
            "equals": {
              "path": "teammates",
              "value": ObjectId("5a9427648b0beebeb69579d0")
            }
          }
        ],
        "minimumShouldMatch": 2
        }
      }
    },
    {
      "$project": {
        "name": 1,
        "_id": 0,
        "score": { "$meta": "searchScore" }
      }
    }
])

You need some kind of loop to map your array of IDs [ id_1 , id_2 , … , id_n ] into the required array but that can simply be done, for example in JS, with:

// untested so some details might be wrong or missing

lots_of_ids = [ 1 , 2 , 3 , 4 , 5 ]
mapped_ids = lots_of_ids.map( id => ( { "equals" : { "path" : "role" , "value" : id } } ) )
db.users.aggregate( [ { "$search":  { "compound": {  "should" : mapped_ids } ] )

yeh, but my point was that if we could filter it in the text search then it would be much better. we don’t need to add an extra loop

how can we use negation in atals search with object id’s?
e.g. normal mongo query { uid: { $nin: blockedUserIds } }

Ok. The question is starting to expand in scope. :slight_smile:

If you don’t mind, please accept the answer(s) that are sufficient for wrapping up this topic. But don’t feel any pressure and only once you feel the concepts are clear.

As for $nin equivalents, you can use a mustNot clause with equals as listed above:

2 Likes

okay, thanks, no I get it, I was wondering if there is any other way to do the same. but I guess we only have this solution. :pray:

query should be value in my query, my apologies.

In my use case, I have data like some property values will be null,
for example:

[
  {
    "id": "1",
    "employeeId": null,
    "staus": false
  },
  {
    "id": "2",
    "employeeId": ObjectId("62c2cd1c9fd30bce6f2ccb20"),
    "staus": true
  }
]

how can I filter property (here employeeId) of type objectId that doesn’t contain null values. (in the end it should return only record with id: '2')

Enjoy my best friend, the documentation.

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