How to search and return all values from mongodb collection whose name field includes a particular value?

Viewed 11

I have a list of units in MongoDB with following schema:

[
   {
      code: Number,
      institutes: [
         name: String,
         code : Number
      ]
      
   }
]

I want to search through the database and return array of all objects whose either institute's name includes req.body.searchValue or whose institute's code includes req.body.searchedValue

What mongodb query can I use for above?

1 Answers
db.getCollection("sample").find(
    {
        "$or" : [
            {
                "institutes.code" : "123"
            },
            {
                "institutes.name" : "BB"
            }
        ]
    }
);
Related