node.js - How to query nested array -


let's have documents like

{     name: "name1",     friends: [         {             name: "name2",             thingswedo: [                 {                     name: "running",                     like_it: true                 },                 {                     name: "swimming",                     like_it: false                 }             ]         }     ] } 

so if want know name of friend both love swimming, how do it?

i know schema design better, if want query schema, how do it?

is possible chain $elemmatch ?

you should $unwind friends , thingswedo in aggregation , use $match match criteria. check below query :

db.collectionname.aggregate({   "$unwind": "$friends" // first unwind friends array  }, {   "$unwind": "$friends.thingswedo" // second unwind friends.thingswedo }, {   "$match": {     "friends.thingswedo.name": "swimming", // match criteria     "friends.thingswedo.like_it": false   } }, {   "$project": {     "name": "$friends.thingswedo.name",     "like_it": "$friends.thingswedo.like_it"   } }).pretty() 

here unwind 2 times large documents query slow down. per recommendation should change schema structure , try avoid unwinding


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -