0

I'm trying to do db.createCollection from Nodejs I'm not getting any error, however, the same command is running in node command window it is throwing COllectionName already exists?

And it is ok to have createCollection command in insert method.

Sample command window query:

=> runs only one and throws error on second run:

db.createCollection("myCollection", function(err, coll){})

The same query doesn't throw any error when running from Nodejs MongoDB driver?

Is it good practice to have createCollection into mine save method(API) reason for asking because it already been created on the first hit of API, so if another time mine API will hit this code again run the same command? Do we need to check if that collection exists if not then only create it?

nitin
  • 156
  • 2
  • 13
  • got the answer for first part of the question... We will receive the error in MongoDB driver also if we set {strict: true} which is default to false in mongo driver. db.createCollection("myCollection", {strict: true}, function(err, coll){}) Please, could anyone give there thought on the second part of the question? – nitin May 06 '18 at 18:19
  • Yes you got the answer. "Is it good practice?" is entirely opinion based and off topic. As you should have gathered by now you don't "need" to call a `createCollection` to simply access it or even create it. If you want to enforce the strictness ( such as where parts of your application pass around a variable used as the collection name ) then that is entirely up to you. Just note that without the strict option you can run the risk of creating a collection name different to what you might expect. – Neil Lunn May 06 '18 at 20:56

0 Answers0