[英]How to projection element in array field of MongoDb collection?
MongoDb Collection Example (Person): MongoDb集合示例(人):
{
"id": "12345",
"schools": [
{
"name": "A",
"zipcode": "12345"
},
{
"name": "B",
"zipcode": "67890"
}
]
}
Desired output: 期望的输出:
{
"id": "12345",
"schools": [
{
"zipcode": "12345"
},
{
"zipcode": "67890"
}
]
}
My current partial code for retrieving all: 我目前检索全部的部分代码:
collection.find({}, {id: true, schools: true})
I am querying the entire collection. 我正在查询整个系列。 But I only want to return zipcode part of school element, not other fields (because the actual school object might contain much more data which I do not need).
但我只想返回学校元素的zipcode部分,而不是其他字段(因为实际的学校对象可能包含更多我不需要的数据)。 I could retrieve all and remove those un-needed fields (like "name" of school) in code, but that's not what I am looking for.
我可以检索所有并删除代码中不需要的字段(如学校的“名称”),但这不是我要找的。 I want to do a MongoDb query.
我想做一个MongoDb查询。
您可以使用点表示法来投影嵌入在数组中的文档内的特定字段。
db.collection.find({},{id:true, "schools.zipcode":1}).pretty()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.