Prashant Angrish
Prashant Angrish

Reputation: 1

Using * in nested mongodb query

I want to search { "info.*.name":"a"}

I have a document below :

id : "1" 
name : "abc"
 info : Object 
   123 : Object
       name : "a" 

id : "1" 
name : "abc"
 info : Object 
   1234 : Object
       name : "a"

id : "1" 
name : "abc"
 info : Object 
   12345: Object
       name : "a"  

I want to search { "info.*.name":"a"}

Upvotes: 0

Views: 51

Answers (1)

Tobok Sitanggang
Tobok Sitanggang

Reputation: 620

You have to put it manually, that would be:

db.document.find({
   info.key.name: "any"
})

and if the key is dynamic, you will need convert it to an array by using aggregation with $objectToArray

Example : https://stackoverflow.com/a/75597562/9267467

Upvotes: 0

Related Questions