【问题标题】:how can i know if connection is safe true?我怎么知道连接是否安全?
【发布时间】:2017-07-07 16:45:32
【问题描述】:

我在mongodb上提出了很多次关于连接的问题,我还不能理解很多东西,但我尝试...

有了这个连接...

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

我得到了一个安全的连接....但是 mongodb 拉我这个警告

========================================================================================
=  Please ensure that you set the default safe variable to one of the                  =
=   allowed values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]     =
=   the default value is false which means the driver receives does                    =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:true})                            =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of false will change to true in the near future                         =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

所以我尝试这种方式......

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

但我不确定这是否是一个安全:真实的连接,所以我这样放

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

可能这样是安全的:true 但是当我将 safe:true 放在 new:true 之前 mongodb 会返回旧的 var,所以我将 safe:true 放在 new:true 之后

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

并且工作正常,但我不确定它是否安全:true,所以我尝试将 safe:true 放入 new:true 对象中

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

我以为 mongdb 吓坏了!但是什么都没有...没有错误没有什么...所以我不知道我怎么知道mongodb何时使用安全:真或不安全:真...

我怎么知道?

【问题讨论】:

    标签: javascript node.js mongodb mongoskin nosql


    【解决方案1】:

    api 不再是 {safe: true} 而是 {w: 1} http://mongodb.github.com/node-mongodb-native/api-generated/db.html

    var db = mongo.db('mongodb://127.0.0.1:27017/test', {w: 1})
    

    {safe: true} 仍然可以使用,但已被弃用。如果设置在 DB 级别,则不需要设置在 collection.insert() 级别。

    插入的api签名是insert(docs[, options][, callback]),所以你应该只有一个选项对象。

    另外,collection.insert 没有 {new: true} 选项。

    所以基本上,您不需要设置任何选项(插入时)。

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多