Which is the best/envisaged way for: Accessing multiple collections

14 views
Skip to first unread message

Kurt Junghanns

unread,
Aug 23, 2016, 5:18:53 AM8/23/16
to node-mongodb-native
Hey guys,
On our project we are using node-mongodb-native 2.1.19 with mode 'use strict'.
In our middleware we have to access multiple collections. We tried to access more than one collection with one connection, but the second execution of db.collection() throws the error: 'collection is not a function' or 'collection is not defined'.
At the moment we have only 2 solutions for this:
  1. store all documents in one collection (bad for indexes and organization)
  2. create a new connection with MongoClient.connect() for each collection (possible performance issues?)

Which is the best/envisaged way (in terms of performance) to work with multiple collection in strict mode (Promises)?

Christian Kvalheim

unread,
Aug 23, 2016, 6:16:39 AM8/23/16
to node-mongodb-native
Why would you use strict mode ? Collections are only created if you perform a write to them (insert/update)

Getting rid of the strict mode makes your life a lot easier as you can just do

db.collection('test').findOne()

and not have to perform an expensive validation check for each time you attempt to access a collection.

Kurt Junghanns

unread,
Aug 23, 2016, 7:04:56 AM8/23/16
to node-mongodb-native
I think my question was false phrased.
The problem with strict mode is not the validation, its the change of the Db object when using it with Promises.
We are using strict mode because we love Promises :)

As strict mode results in a change of a DB (connection) object when a collection was accessed, which is the best way in order to access another collection? (see two solution in first post)

Example:
return MongoClient.connect('')
  .then((dbConnection) => {
    dbConnection.collection('col1').then(...);
    dbConnection.collection('col2').then(...); // <- throws error: collection is undefined
  });


Christian Kvalheim

unread,
Aug 23, 2016, 7:15:26 AM8/23/16
to node-mongodb-native
That does not seem right. If you need to wrap something in a Promise i suggest just doing this instead of using strict which will cause an error if a collection does not exist.

var getCollection = function(db, name) {
  return new Promise(function(resolve, reject) {
    resolve(db.collection(name));
  })

Kurt Junghanns

unread,
Aug 23, 2016, 7:44:54 AM8/23/16
to node-mongodb-native
If strict mode is enabled, everything from node-mongodb-native is already a promise thus no more manual wrapping needed. Or is this a not tested or not suggested way to use node-mongodb-native?
Strict mode is common and helps a lot for code quality. https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Strict_mode Thus there shouldn't be a problem especially node-mongodb-native is a great lib and the most mature one.

The error doesn't say that the at second accessed collection doesnt exists, instead it say that the Db object is missing the function collection which was already used.


Christian Kvalheim

unread,
Aug 23, 2016, 8:09:32 AM8/23/16
to node-mongodb-native
you are confusing strict in "use strict" with strict in the sense of the driver. strict in the driver means it will not return a collection unless that collection already exists. you are getting this error because the driver cannot find one of the collections you are using.

collection('name') does not return a promise because it does not cause any async operation.

Promises should only be used if you are performing something that requires an async IO operation. Otherwise they don't really do anything but add complexity to your code.

Kurt Junghanns

unread,
Aug 30, 2016, 10:56:53 AM8/30/16
to node-mongodb-native
I am not talking about the strict mode of the MongoDB driver. Its really about "use strict" and not passing a callback to the mongodb API.

I have to correct my last comment: As we are using Promises and calling MongoClient.connect('') in a promise which resolves with the connection, everything we do after this (like dbConnection.collection()) is again a Promise which is resolved or rejected.

This seems to break something, after accessing the first collection with one Db object, this Db object is missing the function collection.

Are there performance problems when using for each collection one connection object?
As the documentation says there will be promises returned if no callbacks are provided, what is the preferred way to use Promises with this library?

Christian Kvalheim

unread,
Aug 30, 2016, 11:20:53 AM8/30/16
to node-mongodb-native
The db.collection method does not return promises under any circumstances.


I think that's probably the root of your problem.
Reply all
Reply to author
Forward
0 new messages