MongoDB

MongoDB Create Collection

“We all know that for storing information within a database, we generally create tables. In MongoDB, we call these tables “collections.” These collections work in the exact same way as the tables work in any other DBMS. Moreover, it is extremely simple to create and work with MongoDB collections. Therefore, in this article, we will discuss the method of creating collections in MongoDB in Ubuntu 22.04. Apart from that, we will also learn some other queries associated with MongoDB collections.”

How to Create a Collection in MongoDB in Ubuntu 22.04?

To create a collection (more commonly known as a table) in MongoDB in Ubuntu 22.04, you just need to follow these three simple steps:

Step # 1: Verify the Presence of the MongoDB Server on Ubuntu 22.04

Before doing anything else, you first need to ensure MongoDB is installed on your specific system. For that, you have to check its installed version with the following command:

$ mongo --version

This command will display the version of the MongoDB server if it is installed on your system, as shown below:

After confirming the existence of the MongoDB server on your Ubuntu 22.04 system, you will have to enter its shell by using the “mongo” command. Once you do that, you will easily be able to execute queries within the MongoDB shell.

Step # 2: Create a MongoDB Database in Ubuntu 22.04

Since you can create a collection or a table in MongoDB only if you have a database present on your system, therefore, we will first create a database with the following command:

> use CollectionsDB

We have named our database “CollectionsDB.” Upon the creation of this database, the output shown below was displayed on our MongoDB shell:

Step # 3: Create a MongoDB Collection in Ubuntu 22.04

We can easily create a collection within our newly created MongoDB database. For doing so, we will use the following command:

> db.createCollection(“myFirstMongoDBCollection”)

You do not need to tell this query about the database within which the said collection is supposed to be created. This is so because we have already switched to the target database at its creation in the second step of this method. The name of our MongoDB collection or table is “myFirstMongoDBCollection.”

The success message that appears after the execution of this query is shown in the image below:

It implies that we have successfully managed to create a collection in MongoDB in Ubuntu 22.04.

How to Check All the Collections in MongoDB in Ubuntu 22.04?

If you want to see all the collections that you have created so far within your current database, then you have to run the following query in the MongoDB shell:

> show collections

Since we had only created a single collection within our database, the name of that collection appeared as a result of executing this query. It is shown in the image below:

How to Insert Documents in a MongoDB Collection in Ubuntu 22.04?

Now, we will explain to you the procedure of inserting records or rows within a MongoDB collection which are, in fact, known as “documents”. To insert multiple documents in a MongoDB collection with a single query, you will have to execute the following query:

> db.myFirstMongoDBCollection.insertMany([ { name:“Harry”, age:35 }, { name:“Fred”, age:31 }, { name:“Clara”, age:29 } ])

With the help of this query, we wanted to insert three documents inside our MongoDB collection “myFirstMongoDBCollection.” These documents are comprised of three attributes or columns, each known as “fields” in MongoDB, i.e., name and age. We have used the “insertMany” function to insert multiple documents simultaneously. Moreover, an important point to be highlighted here is that we have explicitly specified the name of our collection while running this query. It is so because a MongoDB database can hold multiple collections inside it. Therefore, it is mandatory to specify which database you wish to insert these documents while inserting the documents. Lastly, you need to be extra cautious with the syntax of the MongoDB queries, including the spacing and the usage of parenthesis. Similarly, you can insert even more than three documents into your MongoDB collection.

If there are no errors in your query, then the specified documents will be successfully inserted into your MongoDB collection, as shown in the image below:

How to View All the Documents in a MongoDB Collection in Ubuntu 22.04?

If you want to view all the documents inserted into a MongoDB collection, then you will have to run the following query:

> db.myFirstMongoDBCollection.find( {} )

All the documents of our current MongoDB collection are shown in the image below:

How to View a Specific Document in a MongoDB Collection in Ubuntu 22.04?

However, at times, you do not want to display all the documents of a collection; rather, you only wish to view a specific document. You can easily do this by making use of any of its fields within the following query:

> db.myFirstMongoDBCollection.find( {age:31} )

In this query, we have used the “age” field of our collection, and we want to extract that document where the age is equal to 31. You can even use the “name” field or any other one if you have it within this query.

The MongoDB document associated with “age:31” is shown in the image below:

Conclusion

Today, we wanted to walk you through the procedure of working with the collections in MongoDB in Ubuntu 22.04. To explain to you, we first taught you what the MongoDB collections are, and then we explained the method of creating collections in MongoDB. After creating a collection in MongoDB, we also taught you how you could insert documents inside a MongoDB collection and view them as per your needs. By going through this guide, you will be able to work around the MongoDB collections in Ubuntu 22.04.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.