Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Defaulting to Strict Mode

Tags:

mongodb

We have a nodejs + mongodb application that has been running in production for several years, and in development on multiple machines. On just one developer's machine, I'm seeing the error

MongoError: collection already exists

Researching this error indicates that this will occur when trying to create an existing collection only if the collection is in strict mode. We don't invoke mongo's strict mode anywhere in our application, and we can only reproduce this error on one machine.

Code which causes this error is as follows:

var mongo = require('mongodb');
mongo.MongoClient.connect(config.mongoConnectionString, {w:1}, function(err, db) {
  db.createCollection('accounts', function(err, collection) {
    // "err" here is the error message.
  });
});

Is there a way to override mongo's default strict: false value? Is there a global configuration option that causes strict mode to be turned on? I would rather not modify the code to specify strict: false for every collection just to enable a single developer. The developer is running mongo v3.2

like image 806
aaaarrgh Avatar asked Jan 28 '16 21:01

aaaarrgh


1 Answers

You can disable 'strict' mode by using this

var mongo = require('mongodb');
mongo.MongoClient.connect(config.mongoConnectionString, {w:1}, function(err, db) {
  db.createCollection('accounts', {strict:false}, function(err, collection) {
    // "err" here is the error message.
  });
});

you can read more about this from here

like image 143
LAV VISHWAKARMA Avatar answered Oct 31 '22 12:10

LAV VISHWAKARMA