Databases and Collections
In MongoDB, databases hold collections of documents.
To select a database to use, in the
mongo shell, issue the
use <db> statement, as in the following example:
If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
MongoDB provides the
db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.
To modify these collection options, see
New in version 3.2.
By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection.
To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.
New in version 3.6.
featureCompatibilityVersion must be set to
"3.6". For more information, see View FeatureCompatibilityVersion.
Collections are assigned an immutable UUID. The collection UUID remains the same across all members of a replica set and shards in a sharded cluster.