javascript - limit records in mongodb query -


this question has answer here:

this document in mongodb , need limit output:

{     "_id": objectid("55880fb8c3addd201ee2f70e"),     "title": "sales",     "level1": [{     "name": "master",     "link": "/sales/master"     }, {     "name": "ecommerce",     "link": "/sales/ecommerce"     }] } 

my search query is:

db.collection.find({     title: "sales",     "level1.name": "master" }, {     "title": 1,     "level1.name": 1,     "level1.name": "master" }) 

this expected output:

{     "_id": objectid("55880fb8c3addd201ee2f70e"),     "title": "sales",     "level1": [{     "name": "master",     "link": "/sales/master"     }] } 

you can use aggregation $unwind level1 array expected result following:

db.collection.aggregate({$unwind:"$level1"},{$match:{"level1.name":"master","title" :"sales"}}) 

if want under 1 array can group :

db.collection.aggregate({$unwind:"$level1"},{$match:{"title" :"sales","level1.name":"master"}},{$group:{_id:"_id","title":{$first:"$title"},"level1":{$push:"$level1"}}}) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -