MONGODB COMMANDS

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.

1

  • 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.

2.png

  • 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.

3.png

  • Let’s check the name of the database were we are  in.

11.png

  • Now I want to create a collection inside the database. So I’m using ‘db.createCollection()‘ for that.

4.png

  • Now let’s check whether it has been created or not.

5

12.png

  • Now I’m going to insert documents to the collection.

6.png

  • Let’s check whether It has been added or not.

7.png

  • We can get the out put in a more readable format using the ‘pretty()‘.

9.png

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

8.png

  • Using the ‘find()‘ we get all the documents. But using ‘findOne()’ we can get only one result.

10.png

  • Also we can search values using something similar to the ‘where‘ clause in MySQL.

13

age == 22

14

age < 23

15

age > 22

16

name != someone

17

name != someone && age == 30

18

age == 22 || age != someone

19.png

  • Also we can limit the output as well.

25.png

  • If you want to skip some search result, then you can do it as well. ‘skip(index)’ is used for this.

26.png

  • Let’s see how to sort a search result. ‘1‘ will sort it in ‘ASC‘ order and ‘-1‘ will sort it in ‘DESC‘ order.

27.png

  • Also we can hide some fields from the output as well. ‘1‘ will show the field and ‘0‘ will hide it.

243.png

  • Now let’s see how to update a specific document in MongoDB.

20.png

21

  • Next I want to remove a document from the collection. We can do it in different ways.

This will remove all the matching documents.

22

This will remove only the first match.

23

  • 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.

28.png

48

49

  • Next I want to drop the collection. It’s really easy to do it.

30

31

  • Next try to drop the whole database.

32.33

  • 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.

36

37

  • 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.

10

11.png

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.

12.png

  • 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.

13.png

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!

Advertisements

5 thoughts on “MONGODB COMMANDS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s