mongodb - Not able to write the query in mongo for array element match? -
i working on project on had make recommendation system users in website.i new mongodb. want retrieve names/id of users have "frnds.type"=1 in below code.
{ "_id" : objectid("56a9fcc15b4e12369150d6ef"), "name" : "udit", "venue" : { "state" : "rajasthan", "city" : "jaipur", "ll" : [ "26.9000", "75.8000" ] }, "lsv" : [ 0.14, 0.18, 0.24, 0.17, 0.05, 0.17, 0.05 ], "username" : "udit", "frnds" : [ { "id" : "amit", "type" : 1 }, { "id" : "nakul", "type" : 0 }, { "id" : "verma", "type" : 1 } ]
}
i have written 1 query giving wrong results
db.users.find({"username":"udit"},{"frnds":{"$elemmatch":{"type":1}}}).pretty()
i want result in manner :
[ { "id":"amit", "type":1 }, { "id":"verma", "type":1 } ]
try aggregation framework below.
db.users.aggregate([{ $match: {username: 'udit'}, {$unwind: '$frnds'}, {$match: {'frnds.type': 1}}, {$group: {frnds: {$push: "$frnds"}} }]);
Comments
Post a Comment