S
S
Sveratum2014-06-23 13:40:12
MongoDB
Sveratum, 2014-06-23 13:40:12

How to connect from Node.Js to replica MongoDB?

Good afternoon!
The situation is as follows, there are three replica servers (separate machines). How to connect to a replica in Nodejs?
I have this now:

var ReplSet = require('mongodb').ReplSet
var Server = require('mongodb').Server
var Db = require('mongodb').Db
var ObjectId = require('mongodb').ObjectID

var replSet = new ReplSet([
    new Server('111.222.333.444', 27017),
    new Server('111.222.333.555', 27017),
    new Server('111.222.333.666', 27017)
  ],
  {rs_name: 'test'}, {read_secondary:true}, {w:2}, {auto_reconnect: true}
);

var db = new Db('GreatDB', replSet);

It connects, reads and writes to the database, but constantly when connected, it issues the following:
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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Vlasenko, 2014-07-06
@srsd

Options is one object, not multiple https://mongodb.github.io/node-mongodb-native/api-...
Should be:

{rs_name: 'test', read_secondary:true, w:2, auto_reconnect: true}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question