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/

http://docs.mongodb.org/manual/aggregation/


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -