MongoDB 聚合查询 - 重命名从嵌入文档中返回的字段 [英] MongoDB Aggregation Query- Rename Fields Returned from Within Embedded Documents

查看:20
本文介绍了MongoDB 聚合查询 - 重命名从嵌入文档中返回的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用聚合运算符来返回具有嵌入(子)文档数组的文档.我想重命名数组的字段名称,并重命名数组嵌入文档中的字段名称.

例如,对于投影,我想将数组从friends"重命名为buddies",并且我还想将嵌入文档中的字段从name"重命名为nickName".我可以在聚合操作中执行此操作吗?如果可以,如何执行?

以下是源文档的示例:

<预><代码>[{_id:对象ID,name: '马修',朋友们: [{名称:'苗条',年龄:'32'},{名称:'布巴',年龄:'36'}]}]

结果应该是这样的:

<预><代码>[{_id:对象ID,name: '马修',小伙伴:【{昵称:'克里斯',年龄:'32'},{昵称:'吉姆',年龄:'36'}]}]

提前感谢您的帮助.

解决方案

对此有几种方法,但这在很大程度上取决于您的 MongoDB 版本.2.6 及更高版本的更新版本支持 $map 运算符,您可以在 $project 做你想做的事:

db.friend.aggregate([{$项目":{名称":1,伙伴":{$地图":{"input": "$friends","as": "el",在": {"nickName": "$$el.name","age": "$$el.age"}}}}}])

在以前的版本中,您将使用 $unwind 处理数组元素并通过 $group:

db.collection.aggregate([{ "$unwind": "$friends" },{$组":{"_id": "$_id","name": { "$first": "$name" },伙伴":{$push":{"nickName": "$friends.name","age": "$friends.age"}}}}])

第一种形式的效率更高一些,因为您不会对数组内容进行反规范化并在管道中生成更多要处理的文档.

I'm currently using aggregate operator to return documents that has an array of embedded (sub) documents. I want to rename the field name for the array and also rename fields names in the embedded documents of the array.

As an example, for projection I want to rename the array from "friends" to "buddies" and I also want to rename fields in the embedded document from "name" to "nickName". Can I do this within an aggregate operation and if so how?

Here's an example of the source document:

[
    {
        _id: ObjectID,
        name: 'Matthew',
        friends: [
            {name: 'Slim', age: '32'},
            {name: 'buba', age: '36'}
        ]
    }
]

Here's what the results should look like:

[
    {
        _id: ObjectID,
        name: 'Matthew',
        buddies: [
            {nickName: 'Chris', age: '32'},
            {nickName: 'Jim', age: '36'}
        ]
    }
]

Thanks for the help in advance.

解决方案

There are a couple of approaches to this, but it largely depends on your MongoDB version. More recent versions from 2.6 and upwards support the $map operator which you can use in $project to do what you want:

db.friend.aggregate([
    { "$project": {
        "name": 1,
        "buddies": {
            "$map": {
                "input": "$friends",
                "as": "el",
                "in": {
                    "nickName": "$$el.name",
                    "age": "$$el.age"
                }
            }
        }
    }}
])

In prior versions you would use $unwind to work with the array elements and re-construct via $group:

db.collection.aggregate([
    { "$unwind": "$friends" },
    { "$group": {
        "_id": "$_id",
        "name": { "$first": "$name" },
        "buddies": {
            "$push": {
                "nickName": "$friends.name",
                "age": "$friends.age"
            }
        }
    }}
])

With the first form being a little more efficient as you are not de-normalizing the array content and producing more documents in the pipeline to process.

这篇关于MongoDB 聚合查询 - 重命名从嵌入文档中返回的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆