nodejs mongoose bulk update

前端 未结 3 810
广开言路
广开言路 2021-02-06 06:02

I\'ve a collection of data and I need to add for every documents a new field. If I run a query to get all documents and the update every single one node.js is stopped, may be fo

3条回答
  •  一整个雨季
    2021-02-06 06:38

    You can drop down to the collection level and do a bulk update. This action will not be atomic - some of the writes can fail and others might succeed - but it will allow you to make these writes in a single round trip to your database.

    It looks like this:

    var bulk = People.collection.initializeUnorderedBulkOp();
    bulk.find({}).update({});
    bulk.find({}).update({});
    ...
    bulk.execute(function(err) {
        ...
    });
    

    Check out the docs here: http://docs.mongodb.org/manual/core/bulk-write-operations/

提交回复
热议问题