1

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)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
andrescabana86
  • 1,778
  • 8
  • 30
  • 56
  • 1
    safe false is fire and forget, it means you do not get a acknowledgement as to when the database itself gets the result, the app just assumes it happened, safe true is the opposite. Normally it is considered best practice to use safe true – Sammaye Feb 20 '13 at 23:20
  • and how use??? in connection or in insert?? or is same thing in both? – andrescabana86 Feb 20 '13 at 23:22
  • That I am unfortunately not sure about, I never used mongoskin :( – Sammaye Feb 20 '13 at 23:24
  • 1
    @andrescabana86 The `safe` option is defined in the underlying [mongodb-native-driver](http://mongodb.github.com/node-mongodb-native/) and is for verifying alterations of data -- [`insert`, `remove`, `save`, `update`, etc.](http://mongodb.github.com/node-mongodb-native/api-generated/collection.html). The key difference is whether it waits to detect errors (`true`) or not (`false`). – Jonathan Lonowski Feb 20 '13 at 23:38
  • thank you! i understand, so i will use only on insert,remove,safe,update but not in .find() or findOne()... but where i have to use safe? in the connection o in the query?? – andrescabana86 Feb 20 '13 at 23:58
  • 2
    @andrescabana86 Those aren't necessarily mutually-exclusive. `{safe: true}` should be set for each "*interaction*" that needs it where waiting is optional (i.e., not querying). If you want both the [connection](http://mongodb.github.com/node-mongodb-native/api-generated/db.html) and a subsequent [insertion](http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#insert) to be "*safe*," specify it for both. – Jonathan Lonowski Feb 21 '13 at 00:08
  • so if i put this??? whats happend... is safe or not??? var db = mongo.db("root:toor@127.0.0.1:27017/cordoba",{safe:true}); db.collection('usuarios').insert(campos, {safe:false},function(err, result) tnx again – andrescabana86 Feb 21 '13 at 00:12
  • 1
    the interaction will not be safe no due to the safe false on the operation itself – Sammaye Feb 21 '13 at 00:22

1 Answers1

2

{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.

  • may be you can help me with this question http://stackoverflow.com/questions/15100544/the-newtrue-option-returns-me-the-old-value-in-mongodb – andrescabana86 Feb 26 '13 at 22:31
  • I don't have permission to add comment to that question. Did you double check the value of "newPub" var before you make the update? Were you able to see the updated document afterwards on the monogo web interface? – Wasula Kankanamge Feb 27 '13 at 15:39