Node.js 使用mongodb时的恼人消息

Node.js 使用mongodb时的恼人消息,node.js,mongodb,Node.js,Mongodb,我使用此代码是为了使用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)

我使用此代码是为了使用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) {});
                });
            }
        });
    }
});
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(函数(err,db){
如果(!err){
log(“连接到'dbname'数据库”);
collection(“items”,{safe:true},函数(err,collection){
log(“开放数据库”);
如果(错误){
log(“items”集合不存在。使用示例数据创建它。”);
var项目=[];
对于(变量i=0;i<10;i++){
推({
标题:“标题”+i,
站点名称:“站点名称”+i,
url:“url”+i,
类型:“类型”+i,
图像:“图像”+i
});
}
db.集合(“项”,函数(错误,集合){
insert(项,{safe:true},函数(err,result){});
});
}
});
}
});
运行应用程序时,我收到以下消息:

========================================================================================
=  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           =
========================================================================================
========================================================================================
=请确保通过设置=
=其中一个选项=
=                                                                                      =
=w:(值>-1或字符串“多数”),其中<1表示=
=无写入确认=
=日记账:真/假,在确认前等待刷新日记账=
=fsync:true/false,在确认之前等待文件系统刷新=
=                                                                                      =
=对于向后兼容性,仍然支持safe,并且=
=允许值为[true | false |{j:true}{w:n,wtimeout:n}{fsync:true}]=
=默认值为false,这意味着驱动程序未收到=
=返回插入/更新/删除成功/错误的信息=
=                                                                                      =
=ex:new Db(新服务器('localhost',27017),{safe:false})=
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+命令=
=                                                                                      =
=无确认默认值将在不久的将来更改=
=                                                                                      =
=当在驱动程序数据库上设置默认安全设置时,此消息将消失=
========================================================================================
此消息是什么?我如何修复它?
是否有更好的方法使用mongodb?

文档可在

改用MongoClient接口


MongoDB正在帮助您在其DB设置中设置默认写关注点(w)参数

换一行就行了

   var db = new mongo.Db('dbname', server, {w:1});
这对于开发/黑客来说很好,但在投入生产之前,您应该了解此选项的后果


Db构造函数有三个参数。“DataBaseName”,ServerObject,{parameters}

显示的消息是关于写入问题的警告,并解释了您得到的选项

 var _mongodb = 'MyDataBase';
 var db = new mongodb.Db(_mongodb, server, {w:'majority'});
参考:


Ref:

lead here得到同样的错误,在我的情况下指向文档并没有多大帮助。请参阅
var MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server;
var mongoClient = new MongoClient(new Server('localhost', 27017));
db = mongoClient.db("mydb");
db.open(function(err, db) {
    if(!err) {
         console.log("Connected to 'mydb' database");
         db.collection('mycollection', {strict:true}, function(err, collection) {
        if (err) {
                console.log("error...");

        }
    });
}
});