liveBook · Manning

Collection

As explained in MongoDB 8.0 in Action, Third Edition

A collection is MongoDB's logical container for related documents.

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains creation, inspection, naming, namespaces, and specialized options with shell commands; does not detail collection storage internals.
Examples
Uses sample_mflix collection listings, a namespace lookup, and a db.createCollection() time-series command.
What it assumes
Introduces the database–collection–document hierarchy before using mongosh against an Atlas deployment.
What you can do after
Operate/configure collections in mongosh by listing them, inspecting their options, and choosing automatic or explicit creation.
How it connects
Connects collections to databases, BSON documents, relational tables, document validation, time-series data, and sharding.

Also explained in

Compare how 16 books explain collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.