mongodb 如何限制mongodb中更新文档的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6590890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:09:06  来源:igfitidea点击:

How to limit number of updating documents in mongodb

mongodb

提问by awsum

How to implement somethings similar to db.collection.find().limit(10)but while updating documents?

如何db.collection.find().limit(10)在更新文档时实现与但类似的功能?

Now I'm using something really crappy like getting documents with db.collection.find().limit()and then updating them.

现在我正在使用一些非常糟糕的东西,比如获取文档db.collection.find().limit()然后更新它们。

In general I wanna to return given number of records and change one field in each of them.

一般来说,我想返回给定数量的记录并更改每个记录中的一个字段。

Thanks.

谢谢。

采纳答案by lobster1234

Unfortunately the workaround you have is the only way to do it AFAIK. There is a boolean flag multiwhich will either update all the matches (when true) or update the 1st match (when false).

不幸的是,您拥有的解决方法是 AFAIK 的唯一方法。有一个布尔标志multi,它将更新所有匹配项 (when true) 或更新第一个匹配项 (when false)。

回答by Nick

You can use:

您可以使用:

db.collection.find().limit(NUMBER_OF_ITEMS_YOU_WANT_TO_UPDATE).forEach(
    function (e) {
        e.fieldToChange = "blah";
        ....
        db.collection.save(e);
    }
);

(Credits for forEach code: MongoDB: Updating documents using data from the same document)

(forEach 代码的积分:MongoDB:使用来自同一文档的数据更新文档

What this will do is only change the number of entries you specify. So if you want to add a field called "newField" with value 1 to only half of your entries inside "collection", for example, you can put in

这只会更改您指定的条目数。因此,例如,如果您想将一个名为“newField”且值为 1 的字段添加到“collection”中只有一半的条目,您可以输入

db.collection.find().limit(db.collection.count() / 2).forEach(
    function (e) {
        e.newField = 1;
        db.collection.save(e);
    }
);

If you then want to make the other half also have "newField" but with value 2, you can do an update with the condition that newField doesn't exist:

如果你想让另一半也有“newField”但值为 2,你可以在 newField 不存在的情况下进行更新:

db.collection.update( { newField : { $exists : false } }, { $set : { newField : 2 } }, {multi : true} );

回答by baylee

Using forEachto individually update each document is slow. You can update the documents in bulk using

使用forEach单独更新每个文档很慢。您可以使用批量更新文档

ids = db.collection.find(<condition>).limit(<limit>).map(
    function(doc) {
        return doc._id;
    }
);
db.collection.updateMany({_id: {$in: ids}}, <update>})

回答by Pablo Cantero

The solutions that iterate over all objects then update them individually are very slow.

迭代所有对象然后单独更新它们的解决方案非常慢。

Retrieving them all then updating simultaneously using $inis more efficient.

检索它们然后同时使用更新$in更有效。

ids = People.where(firstname: 'Pablo').limit(10000).only(:_id).to_a.map(&:id)
People.in(_id: ids).update_all(lastname: 'Cantero')

The query is written using Mongoid, but can be easily rewritten in Mongo Shell as well.

该查询是使用 Mongoid 编写的,但也可以在 Mongo Shell 中轻松重写。

回答by Nonos

As the answer states there is still no way to limit the number of documents to update (or delete) to a value > 1. A workaround to use something like:

正如答案所述,仍然无法将要更新(或删除)的文档数量限制为大于 1 的值。使用类似以下内容的解决方法:

db.collection.find(<condition>).limit(<limit>).forEach(function(doc){db.collection.update({_id:doc._id},{<your update>})})

回答by Andreas Jung

Not sure what the purpose is of applying a limit to updates (not even possible in SQL). As lobster worte: update works only on one document or on all documents matching the update criteria. SO you have to adjust the criteria to your needs or update one by one based on a former query.

不确定对更新应用限制的目的是什么(在 SQL 中甚至不可能)。正如龙虾所说:更新仅适用于一个文档或与更新标准匹配的所有文档。所以你必须根据你的需要调整标准或根据以前的查询一一更新。