Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - What is MongoDBs strict mode and is it a good idea to use?

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
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...