MongoDB stores data records as documents (BSON documents) in collections. A database holds one or more collections.
You can manage databases and
collections using the Atlas UI,
mongosh, or MongoDB Compass. This page covers Atlas UI
procedures. For self-managed deployments, use
mongosh or MongoDB Compass.
Select your client:
MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment. To learn more, see MongoDB Compass.
Databases
Log in to Atlas and go to the Data Explorer page for your project.
Issue the use <db> statement:
use myDB
Start MongoDB Compass and connect to your cluster.
To learn more, see Connect to MongoDB.
Create a Database
In MongoDB Atlas, go to the Data Explorer page for your project
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Data Explorer under the Database heading.
The Data Explorer displays.
Enter the Database Name and the Collection Name
Enter the Database Name and the Collection Name to create the database and its first collection.
If you want to use custom collation on the collection, select the Use Custom Collation checkbox and select the desired collation settings.
Important
Don't include sensitive information in your database and collection names.
For more information on MongoDB database names and collection names, see Naming Restrictions.
Optional. Specify a time series collection
Select whether the collection is a time series collection. If you select to create a time series collection, specify the time field and granularity. You can optionally specify the meta field and the time for old data in the collection to expire.
MongoDB creates the database when you first store data for it. Switch to a non-existent database and run:
use myNewDB db.myNewCollection1.insertOne( { x: 1 } )
insertOne() creates both the
database myNewDB and the collection
myNewCollection1 if they do not already exist. Be
sure that both names follow MongoDB
Naming Restrictions.
Collections
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
Create a Collection
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
In MongoDB Atlas, go to the Data Explorer page for your project
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Data Explorer under the Database heading.
The Data Explorer displays.
Enter the Collection Name.
In the Create Collection dialog box, enter the name of the collection you want to create.
MongoDB Atlas also provides Additional preferences. You can choose from the following options:
Important
Don't include sensitive information in your collection name.
For more information on MongoDB collection names, see Naming Restrictions.
Optional. Specify a time series collection.
Select whether the collection is a time series collection. If you select to create a time series collection, specify the time field and granularity. You can optionally specify the meta field and the time for old data in the collection to expire.
db.myNewCollection2.insertOne( { x: 1 } ) db.myNewCollection3.createIndex( { y: 1 } )
Both insertOne() and
createIndex() create their
respective collection if it does not already exist.
Be sure the collection name follows MongoDB
Naming Restrictions.
Explicit Creation
Use db.createCollection() to explicitly
create a collection with options such as maximum size
or validation rules. Without these options, MongoDB
automatically creates collections when you first store
data.
To modify these collection options, see
collMod.
Schema Validation
By default, documents in a collection do not share a schema. Fields and data types can vary across documents.
You can enforce schema validation rules during insert and update operations.
For MongoDB Atlas deployments, the Performance Advisor and the MongoDB Atlas UI detect common schema design issues and suggest modifications that follow MongoDB best practices. To learn more, see Schema Suggestions.
Modifying Document Structure
To add, remove, or retype fields in a collection's documents, update the existing documents.
Unique Identifiers
Collections are assigned an immutable UUID that remains consistent across all replica set members and shards.
To retrieve the UUID for a collection, run either the
listCollections command or the
db.getCollectionInfos() method.