Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

annoying message when use mongodb

I am using this code in order to use 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) {});
                });
            }
        });
    }
});

When I ran the application I get the message:

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

What is this message and how can I fix it? Is there any better way to use mongodb?

like image 535
Naor Avatar asked Jan 25 '13 16:01

Naor


People also ask

Why do people like MongoDB?

As a document database, MongoDB makes it easy for developers to store structured or unstructured data. It uses a JSON-like format to store documents.

Is MongoDB any good?

MongoDB works best with unstructured data, so it's great for Big Data systems, MapReduce applications, news site forums, and social networking applications. Use MongoDB when: You're using cloud computing.

Is MongoDB used in production?

Success stories in manufacturingCompanies around the world are using MongoDB to scale quickly, develop new products, and increase their speed of innovation. Toyota Material Handling created a smart factory by moving from a monolith to microservices, Microsoft Azure, and MongoDB Atlas.


1 Answers

MongoDB is helpfully instructing you to set default write concern (w) parameter in it's DB settings.

Changing this line should do the trick

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

This will be fine for development/hacking, but you should understand the ramifications of this option before going to production.

MongoDB Write Concern Reference

like image 88
HatAndBeard Avatar answered Sep 23 '22 08:09

HatAndBeard