Batch update with Mongoose

前端 未结 2 2147
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 02:34

I\'m pulling data from a RETS(XML) feed and saving it in a local MongoDB using node and mongoose.

Periodically I need to update the documents and delete the inactive one

相关标签:
2条回答
  • 2021-02-09 03:26

    For completeness, If any one has multiple query conditions and want to add new fields for every matching documents of query condition then we can go with

     var bulk = Person.collection.initializeUnorderedBulkOp();
       bulk.find(query1).update(update1);
       bulk.find(query2).update(update2);
       bulk.execute(callback);
    

    In following documentation It is said that db.collection.initializeUnorderedBulkOp()

    Initializes and returns a new Bulk() operations builder for a collection. The builder constructs an unordered list of write operations that MongoDB executes in bulk. MongoDB executes in parallel the write operations in the list.

    https://docs.mongodb.org/v3.0/reference/method/db.collection.initializeUnorderedBulkOp/

    0 讨论(0)
  • 2021-02-09 03:31

    On MongoDB, to update multiple documents (not just one) using Mongoose you can use the multi option:

    Model.updateMany({ 
      size: 'lage'
    }, { 
      $set: { size: 'large' }
    });
    

    See more on in the Mongoose documentation for updating documents and here

    0 讨论(0)
提交回复
热议问题