Working with Databases and Collections
Learn how to create, drop, and switch between databases. Explore how to create and manage collections within a database.
MongoDB Essentials: Creating Collections
Creating Collections
In MongoDB, collections are analogous to tables in relational databases. They are groups of MongoDB documents. Collections do not enforce a schema, meaning that documents within the same collection can have different fields.
This section will explain how to create collections within a MongoDB database to store documents.
Creating Collections Explicitly
You can create a collection explicitly using the db.createCollection()
method. This method provides options to configure the collection, such as specifying a storage engine or setting document validation rules (schema validation).
Syntax:
db.createCollection(name, options)
name
: A string representing the name of the collection to create.options
: An optional document specifying configuration options for the collection. Common options include:capped
: A boolean that enables a capped collection. Capped collections are fixed-size collections that automatically overwrite their oldest entries when they reach their maximum size.size
: Specifies the maximum size (in bytes) for a capped collection. Required ifcapped
istrue
.max
: Specifies the maximum number of documents allowed in a capped collection.validator
: A document that specifies validation rules for documents inserted into the collection.validationLevel
: Determines how strictly MongoDB applies the validation rules.validationAction
: Determines whether MongoDB should error or warn when a document violates the validation rules.
Example:
To create a collection named users
:
db.createCollection("users")
To create a capped collection named logs
with a maximum size of 10MB:
db.createCollection("logs", { capped: true, size: 10485760 })
Creating Collections Implicitly
MongoDB also creates a collection implicitly when you insert a document into a collection that doesn't yet exist. If you insert a document into a non-existent collection, MongoDB will create the collection automatically.
Example:
db.newCollection.insertOne({ name: "Example Document" })
In this example, if the newCollection
collection does not exist, MongoDB will create it automatically before inserting the document.
Checking if a Collection Exists
You can check if a collection exists using the db.getCollectionNames()
or db.getCollectionInfos()
methods.
Example:
db.getCollectionNames()
This will return an array of all collection names in the current database.
db.getCollectionInfos({name: "users"})
This will return an array of collection information object(s) matching the query. If an object is returned then the collection exists. If the array is empty, the collection does not.
Choosing a Naming Convention
Collection names should be meaningful and follow a consistent naming convention. While MongoDB does not enforce strict rules, it's best practice to use lowercase names and separate words with underscores (e.g., user_profiles
, product_catalog
). Avoid using reserved words or characters.
Summary
You've learned two ways to create collections in MongoDB: explicitly using db.createCollection()
and implicitly by inserting a document into a non-existent collection. Understanding how to create and manage collections is fundamental to organizing and storing data effectively in MongoDB.