mongodb - Find a value inside the document -
i'm facing difficulty create find sentence bring me keys , values - not entire document. document appear this. i'm trying retrieve name , code based on code criteria. example: when place {"code" : { $in : ["js" , "dns"}} , {"name"}} i'd receive like...
[ { "code" : "js", "name" : "java script", "skilltype" : "programming" }, { "code" : "dns", "name" : "dns", "skilltype" : "networking" } ] i mean: array containing part of document, not entire document - , matches criteria. right way utilize find basic need?
below entire document:
{ "_id" : objectid("53a1c40c9b97ebe1e5ad659e"), "type" : "skillsets", "skillset" : [ { "code" : "js", "name" : "java script", "skilltype" : "programming" }, { "code" : "mong", "name" : "mongodb", "skilltype" : "database" }, { "code" : "dns", "name" : "dns", "skilltype" : "networking" } ] }
i believe using aggregation framework can pretty close:
aggregation command, using sample data:
db.collectionname.aggregate([ {$unwind:"$skillset"}, {$match:{"skillset.code":{$in:["dns","js"]}}}, {$project:{skillset:1, _id:0}}, {$group: { _id: null, skill: { $push: "$$root" } }} ]) and result:
{ "_id" : null, "skill" : [ {"skillset" : { "code" : "js", "name" : "java script", "skilltype" : "programming" } }, {"skillset" : { "code" : "dns", "name" : "dns", "skilltype" : "networking" } } ] } you'll want experiment bit total info set exact results need. more on aggregation framework here:
http://docs.mongodb.org/manual/core/aggregation-introduction/
mongodb
No comments:
Post a Comment