Mint

Select, update, delete, create, alter, and drop a table in MongoDB in Linux Mint 20

Select update delete create alter drop a table

Introduction:

MongoDB is a very popular NoSQL database. Like every other database, it also presents you with the different ways, in which you can insert data into this database and can easily manipulate it by performing certain operations on it. However, before learning those operations, you must have this database installed on your system which you can conveniently do by following this helpful tutorial which provides a complete guide of installing MongoDB on Linux Mint 20. Once you have followed this tutorial, you can easily learn to work with tables in MongoDB which we will be explaining in today’s article.

Working with Tables in MongoDB:

Before starting to work with tables in MongoDB, we would like to tell you that the tables in MongoDB are in fact known as “Collections”. The columns or attributes of these collections are known as “Fields” whereas the rows or records of these collections are known as “Documents”. Another point to be noted over here is that you need to take special care of the syntax since any syntax errors will cause your queries to fail and the worst thing is that no detailed information about the exact error is presented on the MongoDB shell, therefore, you will have to figure it out manually which is a very complex task to accomplish. Now we will present to you different examples with the help of which you will get an idea about working with tables or collections in MongoDB while using Linux Mint 20.

Step # 1: Verifying that MongoDB is installed on your Linux Mint 20 System:

For working with anything within MongoDB, you need to have MongoDB installed on your Linux Mint 20 system. You can verify this by running the following command in your terminal:

mongo --version

Running this command will display the version of MongoDB which will indicate that it is installed on your system as shown in the image below:

Step # 2: Entering the MongoDB Shell:

MongoDB has a dedicated shell through which we can work with MongoDB. Therefore, before creating any database and starting to work with it, we need to enter the MongoDB shell by running the following command in the terminal:

mongo

Running this command will immediately take you to the MongoDB shell as shown in the image below:

Step # 3: Creating a Database:

Creating a database is extremely simple in MongoDB. All you have to do is to run the following command in your MongoDB shell:

> use NameOfDatabase

Here, you need to replace NameOfDatabase with whatever name you want for your database. We have named our database as SampleDB.

Once this command manages to create a new database, you will automatically be able to access it since the MongoDB shell will switch from its default database to your newly created database as shown in the image below. It means that now, whatever operations you will perform will be performed on your newly created database.

Step # 4: Creating a Collection:

Now we will attempt to create a collection or a table within our newly created SampleDB by running the following command in our MongoDB shell:

> db.createCollection(“CollectionName”)

Here, you need to replace CollectionName with whatever name you want for your collection. We have named our collection as samplecollection.

If this command will create the collection successfully, then you will receive an { “ok” : 1 } success message as shown in the image below:

Moreover, you can also verify if a collection has been created or not by running the following command in your MongoDB shell:

> show collections

This command is used for displaying the names of all the collections that have been created within a particular database. Since in our case, we only created a single collection i.e. samplecollection, therefore, its name will be displayed on our MongoDB shell as shown in the image below:

Step # 5: Inserting Documents in a Collection:

Now we will try to insert a few records or documents into our samplecollection by running the following query in our MongoDB shell:

> db.CollectionName.insertMany([ { name:”John”, age:24, gender:”Male” }, { name:”Leena”, age:22, gender:”Female” }, { name:”Harry”, age27, gender:”Male” } ]}

Here, you need to replace CollectionName with the name of your particular collection. In our example, “name”, “age”, and “gender” are the three fields or columns of our table or collection whereas the records or documents are enclosed within curly brackets and separated by commas. It means that we have a total of 3 records or documents in this example. However, if you want to add a single document or record, then instead of using the “insertMany” command, you will use the simple “insert” command.

If this query will be executed successfully, you will get an acknowledgment message. Also, the objectIDs of the newly inserted records or documents will be returned as shown in the image below:

Step # 6: Viewing the Documents within a Collection:

Now when we have inserted the data in our collection, we will try to view it. In SQL, we can view the data within a table by using the “SELECT” command whereas, in MongoDB, we make use of the “find” command in the following manner:

> db.CollectionName.find( {} )

Here, you need to replace CollectionName with the name of your particular collection which in our case was samplecollection.

Running this query will return all the documents that you have just inserted into your collection as shown in the image below:

Instead of viewing all the documents, you can also look for a particular document or record within a collection by running the “find” command in the following manner:

> db.CollectionName.find({name:”Leena”})

Here, you need to replace CollectionName with the name of your particular collection. In this example, we wanted to look for the document where the name is “Leena”. You can choose to search by any field or attribute such as age or gender in our case. In either of these cases, the very same document or record will be returned as shown in the image below:

Step # 7: Updating a Document within a Collection:

After viewing the documents within a collection, we will now learn to update any particular document by making use of the “update” command in the following manner:

> db.CollectionName.update( { name:”Leena”}, { $set: { age:20 } } )

Here, you need to replace CollectionName with the name of your particular collection. In the records or documents that we entered previously, Leena’s age was set to “22”. Now we have tried to change it to “20” by executing the query shown above.

If this query is executed successfully i.e. our document is updated successfully, then the output shown in the image below will be displayed on your MongoDB shell:

We can also verify if the age of Leena has been updated by our “update” query or not by running the following command:

> db.CollectionName.find({name:”Leena})

Running this command will display the document where the name is “Leena”. In this document, you will be able to see that her age has been updated to “20” as highlighted in the image shown below:

Step # 8: Deleting a Document from a Collection:

The next operation that we will learn to perform is to delete a record or a document from a collection. This can be done by running the “remove” command in the following manner:

> db.CollectionName.remove( { name:”Harry” } )

Here, you need to replace CollectionName with the name of your particular collection. In this example, we wanted to delete the document where the name is “Harry”.

If this query is executed successfully, then the output shown in the image below will be displayed on our MongoDB shell:

We can also verify if our specified document has been deleted or not by viewing all the documents of our collection by running the following command:

> db.CollectionName.find( {} )

Running this command will display all the remaining documents but the document where the name was “Harry” will no longer be displayed since it has been deleted as shown in the image below:

Step # 9: Deleting a Collection:

Finally, we will try to delete our collection or table by executing the following query:

> db.CollectionName.drop()

Here, you need to replace CollectionName with the name of the collection that you want to delete which in our case is samplecollection.

If this query is executed successfully i.e. our samplecollection is deleted successfully, then a “true” message will be displayed on our MongoDB shell as shown in the image below:

We can further verify if our collection has been deleted or not by displaying all the collections by running the following command:

> show collections

Since we only had one collection and that has also been deleted, therefore, running the above-mentioned command will simply hand over the control back to us without displaying any output as shown in the image below:

Conclusion:

This article presented to you all the basic operations that you can perform on MongoDB tables or collections. By following the very same syntax and operations, you can even write other complex queries that can easily serve the purpose of your particular task.

Similar Posts