Mongoose bulk update operation

Is there a way to make massive updates to the collection in mongoose? The strategy I found used the source collection driver as follows:

var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find(query).update(update);
...
bulk.execute(callback)

However, it bulkis undefined when I do this. Is it just not supported mongoose?

+4
source share
3 answers

. Mongoose .bulkWrite() . API MongoDB, " " " " , , , MongoDB, "Bulk API".

- " ", , , , , .

: mongoose .connect() , , . , , .collection, , , .


, , , mongoose.

, , , . , Db Collection()

.collection , :

mongoose.connection.on('open',function(err,conn) {

   // now it safe to use

   // { .. } Other code
   var bulk = Person.collection.initializeOrderedBulkOp();
   bulk.find(query).update(update);
   bulk.execute(callback)

});

- , , .

Bulk API , , . , , MongoDB 2.6 .

+8

.

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

.
$set

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

- id

var bulk = Person.collection.initializeOrderedBulkOp();
    bulk.find({'_id': id}).update({$set: {status: 'inactive'}});
    bulk.execute(function (error) {
         callback();                   
    });
+3
Person.collection.update(
  {'_id': id}, 
  {$set: {status: 'inactive'}}, 
  {multi: true}, 
  callback
)

in {'_id': id} id - , , where, . {$set: {status: 'inactive'}} - , , ": ". {multi: true} , . callback , .

0

Source: https://habr.com/ru/post/1570019/


All Articles