简体   繁体   English

如何在MongoDb集合的数组字段中投影元素?

[英]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.

相关问题 如何在 MongoDb 集合的多数组字段中投影元素? - How to projection element in multi array field of MongoDb collection? 如何在MongoDB中的投影(项目)中删除值为空的数组元素 - How to remove an array element with value null in projection (project), mongodb MongoDB - 聚合 - 过滤集合中数组字段中的最后一个元素 - MongoDB - Aggregate - Filter on the last element in an array field in collection 如何根据数组元素向 mongoDB 集合的文档添加新字段 - How to add a new field to mongoDB collection's documents based on an array element 如何提取MongoDB聚合中数组元素的字段? - How to extract the field of an array element in MongoDB Aggregation? MONGODB-将集合中的字段添加到Item数组中的字段 - MONGODB - adding a field in collection to a field in Item array 如何根据Mongodb中doc字段中的每个数组项过滤集合 - How to filter a collection based on each array item in a doc field in Mongodb 如何发布mongodb数组长度作为其他收集字段? - How to publish a mongodb array length as an additional collection field? 如何基于MongoDB中的另一个集合嵌入字段值更新嵌入集合字段的数组? - How to update array of embedded collection field based on another collection embedded field value in MongoDB? c# 和 mongodb 使用投影包含或排除数组元素 - c# and mongodb include or exclude element of array using projection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM