KDnuggets Home » News » 2019 » Jun » Tutorials, Overviews » Mongo DB Basics ( 19:n22 )

Mongo DB Basics


Mongo DB is a document oriented NO SQL database unlike HBASE which has a wide column store. The advantage of Document oriented over relation type is the columns can be changed as an when required for each case as opposed to the same column name for all the rows.



By Jyoti Reddy, Data Engineer at Krones

figure-name

Mongo DB is a document oriented NO SQL database unlike HBASE which has a wide column store. The advantage of Document oriented over relation type is the columns can be changed as an when required for each case as opposed to the same column name for all the rows.

 

Ways to operate in Mongo DB

 

1. To create a Database

Use the command — use <desired database name>

 

figure-name

 

 
2. To list the Databases in the Mongo DB system.

Use the command — show dbs

This command will show the default DB’s and the one’s created by the client. However If there are no collections in the DB created then it won’t be shown in the list. Also to check which database is currently selected then use the command- db. The below image shows the database which
currently being operated on.

 

figure-name

 

 
3. Drop Database

Use the command — db.dropDatabase(). Follow the below commands where the databases are shown before and after dropping the selected Database. We cannot specify a selected database name. The Database which is currently selected will be automatically deleted. Also we have inserted some data into sampledb which is why it is appearing in the list. If no data was there in sampledb then would not have appeared after typing the command — show dbs.

 

figure-name

 

 
4. Create Collection

The below process shows show to create a collection in a database. In the Collection lies the document. Use the command — db.collectionname.insert({}). The below image shows only one document created in the collection ‘Country’. We can create multiple documents in the same collection. This collection refers to the Table for HBase or either a relation DBMS.

 

figure-name

 

The below example shows a collection having multiple documents. Notice the number of fields in both the documents. The 1st document does not have the suburb field which means we have to insert only those information which is available with us.

 

figure-name

 

 
5. To find the documents in the collection

Use the command- db.collectionname.find(). The object ID’s would be appended to the each document on it’s own. This would make each document unique.

 

figure-name

 

We can also use AND and OR operator to find specific documents within the collection. For instance to find the document which has the city Mumbai use the below command:

db.country.find({city:’Mumbai’});

 
6. Update the Document within the Collection

To update a particular field use the command-

db.collectionname.update({field:’old name’},{$set:{field:’new name’}})

Before Update

figure-name

 

After Update

figure-name

 

 
7. Delete a Document

To delete a document use the below command-

db.collectioname.remove({field:’Value’})

figure-name

 

We can also use the command db.collectionname.remove({}) to truncate the entire collection and has been added in the Version 2.6. Earlier the command db.collectionname.remove() used to work. This is equivalent to the truncate command in SQL.

 
8. Projection of rows

To select the required fields we can use the below given command. We need to append either ‘1’ or ‘0’ to the field in the command. If we give a ‘1’ then all the rows for that field will appear and ‘0’ will give vice versa results. If the field name is not specified in the command then it default takes a ‘1’ for the Object ID field, this case does not applied for all the other field s in the list. The fields here correspond to the columns in RDBMS.

db.collectionname.find({},field:1})

figure-name

 

 
9. Limiting the rows

This is the advanced version of Projecting the rows. In Projection technique all the rows would appear resulting in us getting unwanted data. Here we can limit the columns as well as skip few of the rows. Use the command -db.collectionname.find({},{field:1 or 0,field:1 or 0…}).limit(1).skip(1). This method will retrieve only one row. the number of skips determines the number of rows that will be removed from the result.

 

figure-name

 

 
10. Sorting the rows

In this method we can sort the rows in the ascending or descending manner. Select the fields that are needed and then append a 1 or -1 to the sort command. Default is 1, hence in such a case the sort command need not be specified. The ascending or descending will be based on the column selected in the Sort command.

Command: db.country.find({},{state:1}).sort({state:-1})

figure-name

 

These are the few methods to operate with Mongo DB. The syntax are user friendly and simple to understand. An another advantage of this database is, we need not include all the fields in all the documents within the same collection. For instance, If one document has a field named city and the other document does not need it then we need not include it. This helps in saving a lot of space. Refer to the below example

 

figure-name

 

Bio: Jyoti Reddy is a Data Engineer at Krones.

Original. Reposted with permission.

Related:


Sign Up

By subscribing you accept KDnuggets Privacy Policy