Databases and Collections in MongoDB (original) (raw)
MongoDB stores data records as documents(BSON documents) incollections. A databaseholds one or more collections.
You can manage databases andcollections using the Atlas UI,mongosh, or MongoDB Compass. This page covers Atlas UI procedures. For self-managed deployments, usemongosh 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, seemongosh.
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 Explorerpage 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 Explorerunder the Database heading.
The Data Explorer displays.
Issue the use <db> statement:
The Databases tab lists the existing databases for your deployment.
- 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.
In the Connections sidebar, select or hover over your cluster and click the icon to open the Create Databasedialog box.
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
For more information on MongoDB database names and collection names, see Naming Restrictions.
Select whether the collection is atime 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.
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 collectionmyNewCollection1 if they do not already exist. Be sure that both names follow MongoDBNaming Restrictions.
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
click to enlarge
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
- 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.
Select or hover over the database, and click the icon to open the Create Collection dialog box.
Select whether the collection is atime 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.
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() andcreateIndex() create their respective collection if it does not already exist. Be sure the collection name follows MongoDBNaming Restrictions.
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, seecollMod.
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, seeSchema Suggestions.
To add, remove, or retype fields in a collection's documents, update the existing documents.
Collections are assigned an immutableUUID that remains consistent across all replica set members and shards.