mongodb - Return only array value in mongo projection -
mongodb - Return only array value in mongo projection -
is there way homecoming value of property in mongodb projection? instance, have document has property value array. want homecoming object query array only, not property: [ .. ]
. example:
document:
db.test.insert({ name: "andrew", attributes: [ { title: "happy"}, { title: "sad" } ] });
query:
db.test.find({name: "andrew"},{attributes:1, "_id":0});
that returns:
{ "attributes" : [ { "title" : "happy" }, { "title" : "sad" } ] }
i want homecoming on array:
[ { title: "happy"}, { title: "sad" } ]
is there way that? thanks
json doesn't allow toplevel array normal query doesn't allow this. can aggregation framework:
> db.test.remove(); > db.test.insert({ name: "andrew", attributes: [ { title: "happy"}, { title: "sad" } ] }); > foo = db.test.aggregate( { $match: { name: "andrew" } }, { $unwind: "$attributes" }, { $project: { _id: 0, title: "$attributes.title" } } ); { "result" : [ { "title" : "happy" }, { "title" : "sad" } ], "ok" : 1 } > foo.result [ { "title" : "happy" }, { "title" : "sad" } ]
this however, not create cursor object find does.
mongodb
Comments
Post a Comment