7

私は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) {});
                });
            }
        });
    }
});

アプリケーションを実行すると、次のメッセージが表示されます。

========================================================================================
=  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           =
========================================================================================

このメッセージとは何ですか?どうすれば修正できますか?mongodbを使用するより良い方法はありますか?

4

4 に答える 4

8

MongoDBは、DB設定でデフォルトの書き込み懸念(w)パラメーターを設定するように指示しています。

この行を変更すると、うまくいくはずです

   var db = new mongo.Db('dbname', server, {w:1});

これは開発/ハッキングには問題ありませんが、本番環境に移行する前に、このオプションの影響を理解する必要があります。

MongoDB書き込み懸念リファレンス

于 2013-12-11T16:41:36.583 に答える
3

Dbコンストラクターには3つの引数があります。"DataBaseName"、ServerObject、{パラメータ}。

表示されるメッセージは、書き込みに関する警告であり、取得したオプションを説明しています。

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

参照: http: //mongodb.github.io/node-mongodb-native/api-generated/db.html

于 2014-02-07T09:41:08.950 に答える
1
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...");

        }
    });
}
});

参照:http://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html

于 2014-08-11T23:22:19.970 に答える
-1

ドキュメントはで利用可能です

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

代わりにMongoClientインターフェースを使用してください

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

于 2013-01-25T16:55:41.913 に答える