What is the difference between safe:true and safe:false in a connection with mongoskin? and how use it?

早过忘川 提交于 2020-01-23 21:28:46

问题


I have a connection with mongoskin and nodejs:

var db = mongo.db("root:toor@127.0.0.1:27017/cordoba");

but I don't know which is the best practice in this case...

db.collection('usuarios').insert(campos,{safe:true}, function(err, result)

I want to insert campos in mongodb, I'm using safe:true... so what happens if I use safe:false, and what is the best practice?

this:

 var db = mongo.db("root:toor@127.0.0.1:27017/cordoba");
 db.collection('usuarios').insert(campos,{safe:true}, function(err, result)

or this:

var db = mongo.db("root:toor@127.0.0.1:27017/cordoba",{safe:true});
db.collection('usuarios').insert(campos, function(err, result)

回答1:


{safe:true} assures you that the callback function will get executed only after the insertion is done and {safe:false} does not guarantee that. I always use {safe:true}, just to make sure that I have the most up to date version of the DB.



来源:https://stackoverflow.com/questions/14991718/what-is-the-difference-between-safetrue-and-safefalse-in-a-connection-with-mon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!