Skip to content

Commit

Permalink
fix(mongodb): prevent "db.collections is not a function" error (#232)
Browse files Browse the repository at this point in the history
What does this PR do / solve?
-----------------------------

Initial goal was to fix the following warning: `DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect`

As a first step, this PR fixes the `db.collections is not a function` error, when trying to run `docker-compose up --build`.

Overview of changes
-------------------

- upgrade the `mongodb` client to a more recent version: `2.2.33`
- switch to the new Mongo URI format

How to test this PR?
--------------------

```
$ docker-compose up --build -d
$ npm run test-docker
```
  • Loading branch information
adrienjoly committed Oct 26, 2019
1 parent b8d4bbe commit 2c353ca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
10 changes: 7 additions & 3 deletions app/models/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ exports.init = function(readyCallback) {
var authStr = '';
if (authUser && authPassword) authStr = authUser + ':' + authPassword + '@';

var url = 'mongodb://' + authStr + host + ':' + port + '/' + dbName; // + "?w=1";
var url = 'mongodb://' + authStr + host + ':' + port; // + "?w=1";

console.log('Connecting to ' + url + '...');
console.log('Connecting to ' + url + '/' + dbName + '...');

var options = {
native_parser: true,
useNewUrlParser: true,
useUnifiedTopology: true,
//strict: false,
//safe: false,
w: 'majority' // write concern: (value of > -1 or the string 'majority'), where < 1 means no write acknowlegement
Expand All @@ -158,9 +160,11 @@ exports.init = function(readyCallback) {
//var dbserver = new mongodb.Server(host, port, {auto_reconnect:true});
//var db = new mongodb.Db(dbName, dbserver, options);

mongodb.MongoClient.connect(url, options, function(err, db) {
mongodb.MongoClient.connect(url, options, function(err, client) {
if (err) throw err;

const db = client.db(dbName);

db.addListener('error', function(e) {
console.log('MongoDB model async error: ', e);
});
Expand Down
11 changes: 5 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"hogan.js": "2.0.0",
"htmlparser": "*",
"iconv": ">=2.0.6",
"mongodb": "^2.2.29",
"mongodb": "^2.2.33",
"object-sizeof": "^1.3.0",
"playemjs": "0.3.0",
"q-set": "^2.0.8",
Expand Down
12 changes: 7 additions & 5 deletions scripts/algolia-search-index/mongodb-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ const Progress = require('./Progress');

var MONGO_OPTIONS = {
native_parser: true,
useNewUrlParser: true,
useUnifiedTopology: true,
//strict: false,
//safe: false,
w: 'majority' // write concern: (value of > -1 or the string 'majority'), where < 1 means no write acknowlegement
};

const makeConnUrl = params => {
var dbName = params.mongoDbDatabase || process.env.MONGODB_DATABASE;
var host = params.mongoDbHost || process.env.MONGODB_HOST;
var port = params.mongoDbPort || process.env.MONGODB_PORT;
var authUser = params.mongoDbAuthUser || process.env.MONGODB_USER;
var authPassword = params.mongoDbAuthPassword || process.env.MONGODB_PASS;
var authStr =
authUser && authPassword ? authUser + ':' + authPassword + '@' : '';
return 'mongodb://' + authStr + host + ':' + port + '/' + dbName;
return 'mongodb://' + authStr + host + ':' + port;
};

// populates db.<collection_name>, for each collection
Expand All @@ -41,14 +42,15 @@ const cacheCollections = function(db, callback) {

const initMongo = (params, callback) => {
var url = makeConnUrl(params);
console.log('Connecting to ' + url + '...');
mongodb.MongoClient.connect(url, MONGO_OPTIONS, (err, db) => {
var dbName = params.mongoDbDatabase || process.env.MONGODB_DATABASE;
console.log('Connecting to ' + url + '/' + dbName + '...');
mongodb.MongoClient.connect(url, MONGO_OPTIONS, (err, client) => {
if (err) {
callback(err);
} else {
const db = client.db(dbName);
db.addListener('error', function(err) {
console.log('MongoDB model async error: ', err);
throw err;
});
cacheCollections(db, callback); // will mutate db and callback
}
Expand Down

0 comments on commit 2c353ca

Please sign in to comment.