HBalyan
HBalyan

Reputation: 273

Mongoose - multiple collection parallel document creation

I am running into a problem when I create documents for different collections. Here the last callback attached to create(...) for each model(corresponding to a collection) is to drop the respective collection but it is not working consistently.

e.g. We have 3 models A, B and C.

db.once('open',function(){

A.create({

...

},function(err,doc){

db.collection('a').drop();

});

B.create({

...

},function(err,doc){

db.collection('b').drop();

});

C.create({

...

},function(err,doc){

db.collection('c').drop();

});

}

All the 3 collections do not get dropped every time.

What could be the reason?

Upvotes: 1

Views: 235

Answers (1)

TGrif
TGrif

Reputation: 5931

The MongoDB documentation says about drop() method:

This method obtains a write lock on the affected database and will block other operations until it has completed.

I suspect this happend to you when you launch simultaneous the three create()/drop() action. You need to control your application flow with some Callback/Promise, otherwise you can't garanty consistent behavior.

Upvotes: 1

Related Questions