Creating a Collection in MongoDB
Creating a Collection in MongoDB
Introduction
In MongoDB, collections are analogous to tables in relational databases. A collection is a grouping of MongoDB documents, and a database can contain multiple collections. This tutorial will guide you through the process of creating a collection using mongosh
.
Prerequisites
MongoDB installed and running on your machine.
Access to
mongosh
. If MongoDB is installed correctly, you can accessmongosh
from the command line.
Step 1: Start mongosh
mongosh
Open Terminal/Command Prompt:
Launch your terminal (macOS/Linux) or command prompt (Windows).
Start
mongosh
:Type the following command and press Enter:
You should see a welcome message and the
mongosh
prompt:
Step 2: Switch to the Database
Before creating a collection, you need to switch to the desired database. If you don’t have a specific database in mind, you can create a new one.
Use an Existing Database or Create a New One:
To switch to an existing database or create a new one (e.g.,
myNewDatabase
), type:You will see:
Step 3: Create a Collection
You can create a collection in MongoDB by using the db.createCollection()
method.
Creating a Collection
Create a Collection:
To create a collection named
users
, run the following command:You should see:
This indicates that the collection
users
has been successfully created.
Automatic Collection Creation
MongoDB also allows automatic collection creation when you insert the first document into a collection. For example:
Insert a Document to Create a Collection:
If you prefer to create a collection by inserting a document, use:
This command will create a collection named
products
if it does not already exist.
Step 4: Verify the Collection
Show Collections:
To check if the collections were created successfully, use:
You should see
users
andproducts
in the list of collections.
Step 5: Insert Documents into the Collection
Now that you have created a collection, you can insert documents into it.
Insert Multiple Documents:
To add multiple documents to the
users
collection, use the following command:
Verify Inserted Documents:
To view the documents in the
users
collection, run:You should see the documents you just inserted.
Step 6: Additional Operations
Dropping a Collection
If you want to delete a collection, you can do so with the drop()
method.
Drop a Collection:
This command will remove the
users
collection and all its documents.
Viewing Collection Statistics
You can also view statistics about a collection, such as the number of documents it contains.
Get Collection Stats:
This command will return statistics about the
users
collection.
Conclusion
You have successfully created a collection in MongoDB using mongosh
, both explicitly and implicitly by inserting documents. You also learned how to verify the collection, insert documents, and perform basic operations. In future tutorials, we will explore querying, updating, and deleting documents within collections.
Feel free to reach out with any questions or for further guidance!
Last updated