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.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform MongoDB related queries.
In this context, we shall look into how to install and configure MongoDB on Linux Mint 20.
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.
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.
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.
Creating a database is extremely simple in MongoDB. All you have to do is to run the following command in your MongoDB shell:
> use Database_Name
Here, you need to replace Database_Name with whatever name you want for your database. We have named our database as NewDB.
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.
This means that now, whatever operations you will perform will be performed on your newly created database.
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.
If this command will create the collection successfully, then you will receive an { “ok” : 1 } success message.
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.
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.
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( {} )
Running this query will return all the documents that you have just inserted into your collection.
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.
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.
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".
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".
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.
Finally, we will try to delete our collection or table by executing the following query:
> db.CollectionName.drop()
If this query is executed successfully i.e. our samplecollection is deleted successfully, then a “true” message will be displayed on our MongoDB shell.
We can further verify if our collection has been deleted or not by displaying all the collections by running the following command:
> show collections
This article will guide you on 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.
MongoDB is almost 100 times faster than traditional database system like RDBMS, which is slower in comparison with the #NoSQL databases. MongoDB supports deep query-ability i.e we can perform dynamic queries on documents using the document-based query language that's nearly as powerful as #SQL.
To open a collection in MongoDB:
1. Finding the current database you're in. db.
2. Listing databases. show databases.
3. Go to a particular #database. use <your_db_name>.
4. Creating a Database.
5. Creating a Collection.
6. Inserting #Data.
7. Querying Data.
8. Updating documents.