使用mongodb时烦人的消息

Nao*_*aor 7 mongodb node.js

我正在使用此代码以使用mongodb:

var mongo = require("mongodb");
var BSON = mongo.BSONPure;

var server = new mongo.Server('localhost', 27017, {auto_reconnect: true, safe: true});
var db = new mongo.Db('dbname', server);

db.open(function(err, db) {
    if(!err) {
        console.log("Connected to 'dbname' database");
        db.collection("items", {safe:true}, function(err, collection) {
            console.log("Open database");
            if (err) {
                console.log("The 'items' collection doesn't exist. Creating it with sample data.");
                var items = [];
                for (var i = 0; i < 10; i++) {
                    items.push({
                        title: "title" + i,
                        site_name: "site_name" + i,
                        url: "url" + i,
                        type: "type" + i,
                        image: "image" + i
                    });
                }
                db.collection("items", function(err, collection) {
                    collection.insert(items, {safe:true}, function(err, result) {});
                });
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我收到消息:

========================================================================================
=  Please ensure that you set the default write concern for the database by setting    =
=   one of the options                                                                 =
=                                                                                      =
=     w: (value of > -1 or the string 'majority'), where < 1 means                     =
=        no write acknowlegement                                                       =
=     journal: true/false, wait for flush to journal before acknowlegement             =
=     fsync: true/false, wait for flush to file system before acknowlegement           =
=                                                                                      =
=  For backward compatibility safe is still supported and                              =
=   allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]      =
=   the default value is false which means the driver receives does not                =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:false})                           =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of no acknowlegement will change in the very near future                =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================
Run Code Online (Sandbox Code Playgroud)

这是什么消息,我该如何解决?有没有更好的方法来使用mongodb?

Hat*_*ard 8

MongoDB有助于指导您在其DB设置中设置默认写入关注(w)参数.

改变这一行应该可以解决问题

   var db = new mongo.Db('dbname', server, {w:1});
Run Code Online (Sandbox Code Playgroud)

这对于开发/黑客来说很好,但是在开始生产之前你应该了解这个选项的后果.

MongoDB写关注参考


小智 -1

该文档位于

http://mongodb.github.com/node-mongodb-native/

使用 MongoClient 接口代替

http://mongodb.github.com/node-mongodb-native/driver-articles/mongoclient.html

  • 导致这里出现同样的错误,在我的情况下指向文档并不是很有帮助。 (4认同)
  • 请参阅http://stackoverflow.com/questions/12846238/node-js-mongodb-set-default-safe-variable (2认同)