Home > front end >  mongodb query $in operator, fetch by ObjectId in Array
mongodb query $in operator, fetch by ObjectId in Array

Time:05-07

All my documents have a structure like this:

{
   operational: {availableFleet: [Objectid('5bad3f452641a1186d21b5f8'), ...]}
}

So every document has an operational key with many other keys inside, one of them being availableFleet which is an Array of multiple ObjectIds

I want to retrieve all documents that contain one specific ObjectId inside the availableFleet Array.

Here's my query:

{operational: {availableFleet: {$in: [ObjectId('5bad3f452641a1186d21b5f8')]}}}

However, it's returning nothing.

I'm using the MongoDB Compass GUI.

CodePudding user response:

Should be:

{'operational.availableFleet': ObjectId('5bad3f452641a1186d21b5f8')}

No need for $in as this is for the case of multiple options for the ObjectId('5bad3f452641a1186d21b5f8').

And nested objects are marked with a .

  • Related