Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/

Databases and Collections in MongoDB

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:

The MongoDB Shell, mongosh, is a JavaScript and Node.js REPL environment for interacting with MongoDB deployments. To learn more, see mongosh.

MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment. To learn more, see MongoDB Compass.

Log in to Atlas and go to the Data Explorer page for your project.

1
  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

2

In the sidebar, click Data Explorer under the Database heading.

The Data Explorer displays.

Issue the use <db> statement:

use myDB
1

To learn more, see Connect to MongoDB.

2

The Databases tab lists the existing databases for your deployment.

1
  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Data Explorer under the Database heading.

    The Data Explorer displays.

2

In the Connections sidebar, select or hover over your cluster and click the icon to open the Create Database dialog box.

3

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.

4

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.

5

Upon successful creation, the database and the collection appears in the Connections sidebar.

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.

1
2
3
4

MongoDB stores documents in collections. Collections are analogous to tables in relational databases.

A collection of MongoDB documents.
click to enlarge

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.

1
  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Data Explorer under the Database heading.

    The Data Explorer displays.

2

Select or hover over the database, and click the icon to open the Create Collection dialog box.

3

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.

4

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.

5

Upon successful creation, the collection appears underneath the database in the Connections sidebar.

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.

1
2
3
4

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.

1
2
3

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.

To add, remove, or retype fields in a collection's documents, update the existing documents.

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.

Back

Documents

Earn a Skill Badge

Master "MongoDB Architecture Essentials" for free!

Learn more

On this page