with mongo db node driver v4.13, how can I load a mongo collection without creating it if not existing?
In earlier versions the function db.collection can be called like this:
db.collection('not_existing', { strict: true }, (err, res) => {
if (err) {
console.log('Collection does not exist');
}
});
But in v4.13 the callback version of this function does not exist anymore and strict: true seems to be ignored.
const collection = await db.collection('not_existing', { strict: true });
console.log(await db.listCollections().toArray()); // lists the collection
You can use
or
to check whether a collection exists or not.