12

I'm working on a node.js app that uses MongoDB and I read this from the docs:

db.collection

Fetch a specific collection (containing the actual collection information). If the application does not use strict mode you can can use it without a callback in the following way.

var collection = db.collection('mycollection');

First of all, what 'strict mode' is the doc referring to?

Also, is it a bad practice to grab the collection in this fashion? Without the callback, wouldn't I lose the ability to capture a potential connection error when trying to select the right collection?

db.collection('some_collection', function(err, collection) {
  // query goes here
});
Community
  • 1
  • 1
doremi
  • 14,921
  • 30
  • 93
  • 148
  • 1
    When I looked at the source code for this strict actually makes sure collections etc are there before returning them, this is good if you have some clause in your app that makes the alternative a security threat I guess – Sammaye Feb 05 '14 at 21:20

3 Answers3

11

http://mongodb.github.io/node-mongodb-native/api-generated/db.html#collection

strict, (Boolean, default:false) returns an error if the collection does not exist

Right there in the documentation.

That is there so your application may not create new collections itself and can only reference what has been created before. Hence the need for the callback, in order to trap the error.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
0

Besides MongoDB's strict mode, there is a smart ORM called mongo-strict that helps you to use MongoDB safely.

https://www.npmjs.com/package/mongo-strict

Mohamed Kamel
  • 282
  • 2
  • 8
-5

It might be referring to Javascript's strict mode instead of a Mongo specific feature. strict mode enables some optional but backwards incompatible changes in the Javascript language that help catch some bugs:

What does "use strict" do in JavaScript, and what is the reasoning behind it?

Community
  • 1
  • 1
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • Yea, I'm aware of Javascript's strict mode, but not necessarily how this changes how I select a Mongo collection nor if this is a good idea. – doremi Feb 05 '14 at 20:45