【问题标题】:Updating an array of objects with a new key in mongoDB使用 mongoDB 中的新键更新对象数组
【发布时间】:2012-12-22 01:05:17
【问题描述】:

类似于这个question

Barrowing数据集,我有类似的东西:

{
    'user_id':'{1231mjnD-32JIjn-3213}',
    'name':'John',
    'campaigns':
        [
            {
                'campaign_id':3221,
                'start_date':'12-01-2012',
            },
            {
                'campaign_id':3222,
                'start_date':'13-01-2012',
            }
        ]
}

我想在campaigns 中添加一个新密钥,如下所示:

{
    'user_id':'{1231mjnD-32JIjn-3213}',
    'name':'John',
    'campaigns':
        [
            {
                'campaign_id':3221,
                'start_date':'12-01-2012',
                'worker_id': '00000'
            },
            {
                'campaign_id':3222,
                'start_date':'13-01-2012',
                'worker_id': '00000'
            }
        ]
}

如何将insert/update 一个新的键插入到一个对象数组中?
我想在数组中的每个对象中添加一个新键,默认值为00000

我试过了:
db.test.update({}, {$set: {'campaigns.worker_id': 00000}}, true, true)
db.test.update({}, {$set: {campaigns: {worker_id': 00000}}}, true, true)

有什么建议吗?

【问题讨论】:

    标签: mongodb insert updates


    【解决方案1】:

    我假设这个操作会发生一次,所以你可以使用脚本来处理它:

    var docs = db.test.find();
    for(var i in docs) {
        var document = docs[i];
    
        for(var j in document.campaigns) {
            var campaign = document.campaigns[j];
            campaign.worker_id = '00000';
        }
    
        db.test.save(document);
    }
    

    脚本将遍历集合中的所有文档,然后遍历每个文档中的所有活动,设置 *worker_id* 属性。 最后,每个文档都被持久化。

    【讨论】:

    • 我将代码放入一个名为addTo.js 的文件中。然后在 mongo shell 中我有mongo addTo.js。错误如下:InternalError: too much recursion src/mongo/shell/collection.js:131。这是正确的方法吗?
    • 您在这个集合中有多少文档?也许你应该多次运行脚本,使用跳过和限制功能来避免这个错误。
    【解决方案2】:
    db.test.update({}, {$set: {'campaigns.0.worker_id': 00000}}, true, true
    

    这将更新 0 个元素。

    如果你想add a new key into every object inside the array 你应该使用: $unwind

    示例:

    {
      title : "this is my title" ,
      author : "bob" ,
      posted : new Date() ,
      pageViews : 5 ,
      tags : [ "fun" , "good" , "fun" ] ,
      comments : [
          { author :"joe" , text : "this is cool" } ,
          { author :"sam" , text : "this is bad" }
      ],
      other : { foo : 5 }
    }
    

    展开标签

    db.article.aggregate(
        { $project : {
            author : 1 ,
            title : 1 ,
            tags : 1
        }},
        { $unwind : "$tags" }
    );
    

    结果:

    {
         "result" : [
                 {
                         "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                         "title" : "this is my title",
                         "author" : "bob",
                         "tags" : "fun"
                 },
                 {
                         "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                         "title" : "this is my title",
                         "author" : "bob",
                         "tags" : "good"
                 },
                 {
                         "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                         "title" : "this is my title",
                         "author" : "bob",
                         "tags" : "fun"
                 }
         ],
         "OK" : 1
    }
    

    在您可以编写简单的更新查询之后。

    【讨论】:

    • 必须通过脚本而不是通过 mongo shell 添加新密钥?
    • 如果你可以通过 mongo shell 做到这一点,这意味着你可以通过每个 mongo 驱动程序使用它。
    • 你可以通过mongo shell来做吗?据我所知aggregate 不会修改数据库,而是输出请求的文档。我想修改实际的数据库。
    • 这只是一个例子。我想给你点解决你的问题。 api.mongodb.org/python/current/examples/aggregation.html
    • 我理解并感谢您,但我正在寻找通过 mongo shell 修改文档的东西。我确信应用脚本可以解决这个问题,但我想直接使用 mongo shell。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2015-05-12
    • 1970-01-01
    相关资源
    最近更新 更多