In order to retrieve a Collection object for an existing
collection stored in the database call the
getCollection()
function from a Schema
object.
// Get a collection object for 'my_collection'
var myColl = db.getCollection('my_collection');
The createCollection()
, together with the
ReuseExistingObject
field set to true, can be
used to create a new collection or reuse an existing collection
with the given name. See Section 4.2.1, “Creating a Collection”
for details.
In most cases it is good practice to create database objects during development time and refrain from creating them on the fly during the production phase of a database project. Therefore it is best to separate the code that creates the collections in the database from the actual user application code.