如何在 MongoDB 的搜索/投影中重命名字段?

68

如何在查询数据时修改返回字段的名称?我想使用类似于$rename的方法,但是我不想改变我正在访问的文档。我只想以不同的方式检索它们,就像在SQL中使用SELECT COORINATES AS COORDS一样。

我现在的做法:

db.tweets.findOne({}, {'level1.level2.coordinates': 1, _id:0})
{'level1': {'level2': {'coordinates': [10, 20]}}}

我希望返回的内容是:{'coords': [10, 20]}

4个回答

84

基本上是使用.aggregate()而不是.find()

db.tweets.aggregate([
    { "$project": {
        "_id": 0,
        "coords": "$level1.level2.coordinates"
    }}
])

这样就可以得到您想要的结果。

MongoDB 2.6及以上版本会像find一样返回一个“游标”。

更多详细信息请参见$project和其他聚合框架运算符


对于大多数情况,处理游标时应该只需重命名从.find()返回的字段。以JavaScript为例,您可以使用.map()来完成此操作。

从shell中:

db.tweets.find({},{'level1.level2.coordinates': 1, _id:0}).map( doc => {
  doc.coords = doc['level1']['level2'].coordinates;
  delete doc['level1'];
  return doc;
})

更内联的写法:

db.tweets.find({},{'level1.level2.coordinates': 1, _id:0}).map( doc => 
  ({ coords: doc['level1']['level2'].coordinates })
)

这样可以避免服务器上的任何额外开销,并且应该在这种情况下使用,即额外处理开销会超过实际减小检索数据大小的收益。在这种情况下(以及大多数情况下),它将是最小的,因此更好地重新处理游标结果以进行重构。


7

众所周知,通常在项目阶段中,$project使用字段名称并指定1或0/true或false来确定是否在输出中包含该字段,我们还可以针对一个字段指定值而不是true或false来重命名该字段。以下是语法:

    db.test_collection.aggregate([
        {$group: {
            _id: '$field_to_group',
            totalCount: {$sum: 1}
        }},
        {$project: {
            _id: false,
            renamed_field: '$_id',    // here assigning a value instead of 0 or 1 / true or false effectively renames the field.
            totalCount: true
        }}
    ])

6

如@Neil Lunn所述,可以使用聚合管道来实现此操作:

并且从 Mongo 4.2 开始,可以使用$replaceWith 聚合运算符来将文档替换为子文档:

// { level1: { level2: { coordinates: [10, 20] }, b: 4 }, a: 3 }
db.collection.aggregate(
  { $replaceWith: { coords: "$level1.level2.coordinates" } }
)
// { "coords" : [ 10, 20 ] }

既然你提到了findOne,你也可以通过以下方式将结果文档的数量限制为1:

db.collection.aggregate([
  { $replaceWith: { coords: "$level1.level2.coordinates" } },
  { $limit: 1 }
])

在Mongo 4.2之前和开始使用Mongo 3.4时,可以使用$replaceRoot代替$replaceWith

db.collection.aggregate(
  { $replaceRoot: { newRoot: { coords: "$level1.level2.coordinates" } } }
)

1

阶段(>= 4.2)

  • $addFields : {"New": "$Old"}
  • $unset : {"$Old": 1}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接