Compound Query filter by multiple objectId

{
  index: 'userNameCity',
  compound:{
    
    filter:{
      
      equals: {
value: ObjectId [ '6271f2bb79cd80194c81f631', '62cf35ce62effdc429efa9b0' ],
       path: '_id'
  },
   
    }
 ,

The index for the path (_id) is of datatype ObjectID
I want to filter by an array of objectId.
The above code works for single objectId but does not work for array
Note:
The array will be a dynamic list

Can you please suggest

1 Like

The syntax

is most likely wrong.

Try [ObjectId(‘627…631’),ObjectIs(‘62c…9b0’)]

@steevej Thanks for the suggestion however it does not work

Hi @Manoranjan_Bhol ,

Thanks for your response.

Please note that, values in the equals filter accept either boolean or objectId as a parameter. Please refer to the documentation. Hence you should mention either boolean or ObjectId value only but cannot mention Array or string there.

Thanks,
Darshan

Take a look at https://www.mongodb.com/docs/atlas/atlas-search/compound/, may be you can use it with filter to achieve what you want to do.

@steevej @DarshanJayarama
Thank you for the help again

My goal is to

  • filter by multipls objectId(s)
  • then perform autocomplete search on name, city etc

Hi @Manoranjan_Bhol ,

Thanks for your reply.
Please note that, in index definition If you have created _id with type: objectId then in the equals you can not have more than 1 objectId as that accept either only single objectId in the query.

Further, I have noticed you are mentioning 2 index in the query, that create ambiguity, Do you mind sharing the index definition of this string index.

Thanks,
Darshan

@DarshanJayarama

Please refer to the index defined

So any thoughts? Or workarounds on how to implement functionality like described above? Is the only way is use Compound with hundreds of equals operators??

I came across this thread recently while I was trying to narrow down Atlas Text Search results based on an ObjectId status field. In my query I’d have 1, 2, 3, or 4 ObjectId values to check against the status field.

I was able to achieve what I wanted by adding the in operator to the must term in my compound query. I’m not sure this is exactly what the op was trying to do but sharing here anyway!

Something like this did the trick:

[
  {
    $search: {
      index: "default",
      compound: {
        must: [
          {
            text: {
              query: "some search term",
              path: {
                wildcard: "*",
              },
            },
          },
          {
            in: {
              path: "status",
              value: [
                "6271f2bb79cd80194c81f631",
                "62cf35ce62effdc429efa9b0",
              ],
            },
          },
        ],
      },
    },
  },
]