更新mongodb中数组中的所有元素 [英] Update all elements in an array in mongodb

查看:452
本文介绍了更新mongodb中数组中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mongodb文档,例如:

I have an mongodb document like:

{
    "name" : "JohnDoe",
    "adds" : [
        {
            "status" : "PENDING",
            "date" : "2015-09-23"
        },
        {
            "status" : "PENDING",
            "date" : "2015-10-01"
        }
    ]
}

我想更新所有元素数组,例如:

I want to update all the elements array like:

collection.update({'name':'JohnDoe'}, {'$set':{'adds.status':'APPROVED'}}).

该怎么做?

大多数解决方案说使用位置运算符选择数组元素,然后使用运算符更新该元素.但就我而言,我必须更新数组中的每个元素.

Most solution says to use positional operator to select the array element then use the operator to update that element. But In my case i have to update each element in array.

推荐答案

如前所述,主要问题是长期存在的问题中记录的位置运算符对多个元素的更新: http://jira.mongodb.org/browse/SERVER-1243

As mentioned, the main issue here is with updates on multiple elements with the positional operator as recorded in this long standing issue: http://jira.mongodb.org/browse/SERVER-1243

因此,基本情况是没有一个执行可以执行此操作,因此,要处理多个数组元素,您需要某种方法来确定需要更新多少个元素,并为每个元素处理一个更新语句.

The basic case therefore is no single execution can do this, so in order to process for multiple array elements you need some method of determining how many elements you need to update and process one update statement per each element.

一种简化的方法通常是使用批量操作进行处理最终以多个"更新操作作为单个请求和对服务器的响应:

A simplified approach to this is generally using Bulk Operations to process what ends up being "mulitple" update operations as a single request and response to the server:

var bulk = db.collection.initializeOrderedBulkOp(),
    count = 0;

db.collection.find({ "name": "John Doe", "adds.status": "PENDING" }).forEach(function(doc) { 
    doc.adds.filter(function(add){ return add.status = "PENDING" }).forEach(function(add) {
        bulk.find({ "_id": doc._id, "adds.status": "PENDING" }).updateOne({
            "$set": { "adds.$.status": "APPROVED" }
        });
        count++;

        // Execute once in 1000 statements created and re-init
        if ( count % 1000 == 0 ) {
            bulk.execute();
            bulk = db.collection.initializeOrderedBulkOp();
        }
    });
});

// Execute any pending operations
if ( count % 1000 != 0 )
    bulk.execute();

如果更新的文档很小,或者实际上只有一个文档,则可以放弃count检查,只需将所有批量更新附加到所需的循环中,然后在所有循环结束时执行一次即可.

If your updated documents is quite small, or indeed only a single document then you can forgo the count check and simply append all bulk updates within the required loops and just execute once at the end of all loops.

可以在如何更新多个数组元素中找到更详细的解释和替代方法,但是所有这些都可以归结为不同的方法匹配元素以更新和处理位置$ 为匹配的每个文档更新更新时间,或者直到不再返回修改后的文档为止.

A longer explaination and alternates can be found on How to Update Multiple Array Elements, but all come down to different approaches to matching the element to update and processing a positional $ update mutliple times, for either each document matched or until there are no more modified documents returned.

这篇关于更新mongodb中数组中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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