Batch update with Mongoose

前端 未结 2 2148
伪装坚强ぢ
伪装坚强ぢ 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/

提交回复
热议问题