0

I have a collection named Teachers on MongoDB with some documents, here is an example:

{
"_id" : ObjectId("63a89cdae570d47034c996ec"),  
"teachingCategories" : [ 
    [ 
        ObjectId("63a40c7e54351f6071b25abd"), 
        ObjectId("63a65195075c0031b1bd2e81")
    ], 
    [ 
        ObjectId("63a40b4654351f6071b25a82"), 
        ObjectId("63a41d5d54351f6071b25acf"), 
        ObjectId("63a44bdc54351f6071b25b9a")
    ], 
    [ 
        ObjectId("63a40c7e54351f6071b25abd"), 
        ObjectId("63a651d6075c0031b1bd2e87")
    ]
]

}

i am trying to find the teachers who have the category id like below

db.collection("teachers").find({ teachingCategories: ObjectId("63a44bdc54351f6071b25b9a") })

but the mongo db returns nothing

Nimer Farahty
  • 1,494
  • 2
  • 7
  • 8

1 Answers1

1

As the teachingCategories in your document is arrays in array you can try something like this

db.teachers.find({
  teachingCategories: {
    $elemMatch: {
      $elemMatch: {
        $in: [ObjectId("63a44bdc54351f6071b25b9a")]
      }
    }
  }
})

Similar question to Querying an array of arrays in MongoDB