简体   繁体   English

猫鼬-多个集合并行文档创建

[英]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. 在这里,每个模型(对应于一个集合)与create(...)关联的最后一个回调是删除相应的集合,但它不能始终如一地工作。

eg We have 3 models A, B and C. 例如,我们有3个模型A,B和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. 这3个集合不会每次都掉落。

What could be the reason? 可能是什么原因?

The MongoDB documentation says about drop() method: MongoDB文档介绍了有关drop()方法的信息:

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. 我怀疑这是在您同时启动三个create()/ drop()操作时发生的。 You need to control your application flow with some Callback/Promise, otherwise you can't garanty consistent behavior. 您需要通过一些“回调/承诺”来控制应用程序流,否则您将无法保证一致的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM