Db.collection no longer supports strict mode, so how can I assure that my code isn't making a new collection?

The latest changes in node-mongodb-native include deprecation of the {strict:true} option of the Db.collection method. I’ve failed to find any guidance for how to write code that is assured to only work with existing collections without using {strict:true}. Can you point me at something that shows the proper way to do this in the future?

1 Like

Hello @John_Bito, welcome to the MongoDB Community forum!

The latest MongoDB NodeJS Driver v3.6 does have the Db.collection method working fine with both the strict: true and false (the default) options. It is not deprecated.

Please tell what is the version of NodeJS, MongoDB server and the MongoDB NodeJS Driver you are working with? You can also include the code you had tried along with the actual error / warning message you are seeing.

I guess the deprecation may not be released, as it was merged about 4 weeks ago, after 3.6.9 was finished. The Jira ticket doesn’t provide any rationale for this change, though it’s linked to the removal of the strict option from Db.createCollection saying the strict option has no effect on that method’s behavior.

The strict option does change the behavior of Db.collection. Perhaps the rationale is that it appears to provide some advantage over calling Db.listCollections separately when there’s not an actual advantage.

1 Like

So now, if you try to use {strict: true} on createCollection() you will get an error:
MongoServerError: BSON field ‘create.strict’ is an unknown field.

From this ticket: https://jira.mongodb.org/browse/NODE-2752, looks like it is expected now.

Thus, could you guys please update documentation, that “strict” option is not allowed anymore:
http://mongodb.github.io/node-mongodb-native/3.1/api/Db.html#createCollection

Thanks

1 Like

We could really use a solution to this. The strict option was a very easy way to ensure that we were working on an existing collection. They provided a deprecation message but did not state an alternative or solution. If possible, could anyone please give a solution on how to ensure the collection exists using db.collection? Thank you.