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
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.
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.