'mongodb query $in operator, fetch by ObjectId in Array
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.
Solution 1:[1]
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 .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nimrod serok |