nodejs mongoose bulk update

前端 未结 3 812
广开言路
广开言路 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:54

    More detailed info about the query and update query.

    var bulk = People.collection.initializeOrderedBulkOp();
        bulk.find(query).update(update);
        bulk.execute(function (error) {
           callback();                   
        });
    

    Query is searching with array.
    Update needs a $set

    var bulk = People.collection.initializeOrderedBulkOp();
        bulk.find({'_id': {$in: []}}).update({$set: {status: 'active'}});
        bulk.execute(function (error) {
             callback();                   
        });
    

    Query is a searching the id

    var bulk = People.collection.initializeOrderedBulkOp();
        bulk.find({'_id': id}).update({$set: {status: 'inactive'}});
        bulk.execute(function (error) {
             callback();                   
        });
    

提交回复
热议问题