MongoDB get only matching fields from array -
i trying make distinc query. collection readdata
{"searchphrases" : [ "cheap samsung mobile", "red nokia 2mp" ] } { "searchphrases" : [ "red peter england shirt", "redish lee pant" ] }
i tried query db.readdata.distinct("searchphrases",{"searchphrases":"cheap samsung mobile"});
gives me out put [ "cheap samsung mobile", "red nokia 2mp" ]
expecting output [ "cheap samsung mobile"]. can't iterate manually because important data search. , huge collection so. other way using distinct?
can use aggregation framework, if yes below
db.readdata.aggregate( { $match: {"searchphrases": { $in : ["cheap samsung mobile"] } } }, { $unwind: $searchphrases }, { $match: {"searchphrases": { $in : ["cheap samsung mobile"] } } }, { $group: { _id:"$searchphrases" } } )
this might not exact code. references here:
http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/
Comments
Post a Comment