A collection is a MongoDB database’s container for documents. It is roughly analogous to a relational table, but its records are BSON documents rather than rows. Documents in a collection can contain arrays and nested documents, and their structures can evolve without a predefined table schema. section 2.6 section 2.6.4 section 2.7
Choose how to create and inspect a collection:
- For an ordinary collection, store data with insertOne(); MongoDB creates the collection if it does not already exist. createIndex() can also create it. section 2.6.3
- For creation options such as a capped size, time-series settings, or document validation, call db.createCollection() explicitly. section 2.6.3
- In mongosh, select a database with use sample_mflix, then run db.getCollectionNames() or show collections. Run db.getCollectionInfos() to inspect creation options, permissions, and indexes. section 2.6.3
Address a collection
A collection’s namespace joins its database and collection names with a dot: sample_mflix.embedded_movies. In mongosh, db.getCollection("embedded_movies") accesses that collection. A namespace is limited to 255 bytes; collection names cannot be empty or begin with the reserved system prefix. section 2.6.3
Listing 2.4 Creating a time-series collection
db.createCollection(
"my_time_series_collection",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata"
}})
This db.createCollection() example supplies a time-series timeField and metaField—options that call for explicit creation rather than relying on the first insert. section 2.6.3
Collection type and deployment affect behavior. Capped collections have a fixed size and overwrite the oldest documents when full; time-series collections organize time-based data for efficient storage and queries. In a sharded cluster, MongoDB distributes data at the collection level, but not every collection must be sharded. section 2.7 section 9.3.1















