'insert with "error on non existing collection" semantics
The usual behavior in MongoDB is that collections are created upon first document insertion, which is fine for most of the use cases. However, in some cases you want to check if collection previously exists.
It seems the recommended way of doing this is to rely on listCollections to check for collection existence. However, this would involve to do a listCollections operation each time a document is inserted, thus duplicating the number of operations done in the database.
Is there any way of running insert() (and/or derived methods, such as insertMany(), etc.) with "error on non existing collection" semantics or, in general, other alternative to per-insert checking with listCollections?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
