Hi all, from a previous post we talked about how to install MongoDB, how to start the server, how to connect as a client and how to stop the server. From this post I’m going to introduce you some of the basic coomands used in MongoDB to create, update, delete databases, collection and also data manipulations. From a previous post I talked about all of these for MySQL as well. Let’s see each of them with examples for MongoDB.
- First of all I’m going to start the server and connect to it.
- ‘show dbs‘ is the command to view existing data bases. If this is the first time you are going to work on MongoDB, then there will be only one database.
- Then I’m going to create a database called ‘mydb‘. We can use the ‘use’ command to do that. If the database is existing, then it will access the database and otherwise it will create it.
- Let’s check the name of the database were we are in.
- Now I want to create a collection inside the database. So I’m using ‘db.createCollection()‘ for that.
- Now let’s check whether it has been created or not.
- Now I’m going to insert documents to the collection.
- Let’s check whether It has been added or not.
- We can get the out put in a more readable format using the ‘pretty()‘.
If you have noticed, there is a new key : value pair called ‘_id‘. This is the default primary key of the MongoDB and it is added automatically and assigns a value if we don’t assign a value for it. It contains 12 bytes.
- a 4-byte value representing the timestamp
- a 3-byte machine identifier
- a 2-byte process id
- a 3-byte counter starting with a random value and increments
- Using the ‘find()‘ we get all the documents. But using ‘findOne()’ we can get only one result.
- Also we can search values using something similar to the ‘where‘ clause in MySQL.
age == 22
age < 23
age > 22
name != someone
name != someone && age == 30
age == 22 || age != someone
- Also we can limit the output as well.
- If you want to skip some search result, then you can do it as well. ‘skip(index)’ is used for this.
- Let’s see how to sort a search result. ‘1‘ will sort it in ‘ASC‘ order and ‘-1‘ will sort it in ‘DESC‘ order.
- Also we can hide some fields from the output as well. ‘1‘ will show the field and ‘0‘ will hide it.
- Now let’s see how to update a specific document in MongoDB.
- Next I want to remove a document from the collection. We can do it in different ways.
This will remove all the matching documents.
This will remove only the first match.
- Next let’s see how to get a backup in MongoDB. For this we have to run ‘mongodump‘ which is located at ‘/mongo-installed-directory/bin/‘. ‘–out‘ is the output directory. ‘–collection‘ name of the collection to be dumped and ‘–db‘ name of the database to be dumped.
- Next I want to drop the collection. It’s really easy to do it.
- Next try to drop the whole database.
- Finally let’s have a look at how to import a database from a dump. For this you have to run the ‘mongorestore‘ which is located at ‘/mongo-installed-directory/bin/‘. ‘–dir‘ is the directory where the dump is stored. ‘-d‘ is the name of the database and the ‘-c‘ gives the name of the collection to be restored.
- Now let’s see how to add a user to MongoDB and how to do authentication. For do this we have to use the admin data base and then add a user to it.
Here the first parameter is the user name, then the password and then the roles. Here we can give different roles for different database for a particular user.
- Finally we can check whether the user has been added or not using the auth(). If it’s 1, then the user has been added. If it’s 0, there is a problem.
That’s all about the MongoDB. Hope now you have a clear idea about MongoDB. Hope to see you soon with another important topic. Thank You!