Reputation: 684
I am working on project on which I had to make recommendation system for users in website.I am new to Mongodb. I want to retrieve the names/id of users who 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 one query but it is giving wrong results
db.users.find({"username":"udit"},{"frnds":{"$elemMatch":{"type":1}}}).pretty()
I want result in this manner :
[ { "id":"amit", "type":1 }, { "id":"verma", "type":1 } ]
Upvotes: 0
Views: 37
Reputation: 48356
Try with Aggregation Framework as below.
db.users.aggregate([{
$match: {username: 'udit'},
{$unwind: '$frnds'},
{$match: {'frnds.type': 1}},
{$group: {frnds: {$push: "$frnds"}}
}]);
Upvotes: 1