Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Wednesday, January 27, 2021

MongoDB Aggregation

Student Collection:  Download Student.json

// Function to Count no. of Documents in a Collection
db.student.count()


db.student.aggregate([
    //stage-1
    {$match: {sec:"B"} },
    //stage-2 
    {$count"Total-Student in Sec:B"}
])

db.student.aggregate([
    //stage-1
     {$match: {sec:"B"} },
     //stage-2
     {$group: {_id:"$gender"total_st: {$sum:1}, max_age:{$max:"$age"} } },
     //stage-3
     {$match: {_id:"male"}},
       //stage-4
     {$project: {"max_age":1}}
 ])


 db.student.aggregate([
     //stage-1
     {$group:{_id:"$sec"total_stu:{$sum:1}}},
     //stage-2
     {$sort: {_id:1}},
     //stage-3
     {$skip:1},
     //stage-4
     {$limit:2},
     //stage-5
     {$out"FilteredCollection"}
 ])


// -------------------Example------------------------
db.skill.insert([
    {name:"pankaj"skills:["c","c++""python""java""web"]},
    {name:"sachin"skills:["web""python""java"]},
    {name:"manish"skills:["ml""Iot""web"]}
])

Problem: Find the name of the students who are having "web" as their skills.

db.skill.find({skills:"web"})

// Using Aggregation

db.skill.aggregate([
    {$group:"$skills"}
])

// ----
// $unwind stage
// ----

db.skill.aggregate([
    {$unwind:"$skills"}
])

// ----
// $unwind and group stage
// ----

db.skill.aggregate([
    {$unwind:"$skills"}
    {$group:{_id:"$skills"}}
])

// --------Solution to the Problem ------------------------

db.skill.aggregate([
    {$unwind:"$skills"},
    {$group:{_id:"$skills",count:{$sum:1}, names:{$push:"$name"}}}
    {$match:{_id:"web"}}
])


Wednesday, May 13, 2020

Data Backup and Restoration

Data backup is one of the vital and highly required process for any database management system. The primary reason being is that it is difficult to anticipate how and when the data can be lost. So it is an ideal and best practice that whenever a database is setup, we need to ensure that it has a provision for the data backup in case of any loss events happens.

A backup is nothing but the copy of data from the database which helps in reusing the database in case of any catastrophic event happens to the data.

MongoDB: Taking Data Backup

In order to perform a data backup process in mongodb, the command mongodump should be used. This command will simply dump all the data stored into a dump directory of the mongodb. It also helps to backup the data from the remote servers as well.

In order to properly perform the data backup, follow the below mentioned instructions:

  1. Start the mongodb server with the command mongod
  2. Start the mongodb client with the command mongo in a new command prompt.
  3. Switch to the required collection and run the command mongodump. Since the mongodump is not actually command from mongodb shell, you need to execute the command as shown below.

  1. From the above screenshot observer that all the data of the mongodb database will get backup.

Mongodump basically reads the data from the database and creates a BSON file in which the data is dumped. Mongodump writes only documents from the database. The resultant backup of the data will be of the space efficient. The backup of the data will be stored under the mongodb's bin\dump folder.

Also, there is one disadvantage of using mongodump which will have some performance impact when the data of a collection is huge than the available system memory.

MongoDB: Restoring Data from Backup

Now let us learn how to restore the backup data in mongodb. Data backup is basically used to reconstruct the data in case of a loss event. MongoDB helps to restore the backup data through its one of the utility tools called mongorestore which in turn is a command as well.

The below screenshot shows how the backup data is restored using the command mongorestore in MongoDB.

The above screenshot shows that the dumped collection data has been restored successfully.

Backup and restore Selected Database

To dump and restore selected database, use following commands:

C:\mongodb\bin>mongodump --db studentdb

This will backup only the selected database studentdb instead of backup the entire MongoDB databases.

C:\mongodb\bin>mongorestore --db studentdb dump/studentdb

This will restore only the selected database studentdb instead of restoring the entire MongoDB databases.

Backup and restore Selected Collection of a Database

To backup and restore selected database’s Collection, use following commands:

C:\mongodb\bin>mongodump --db studentdb --collection address

This will backup only the selected collection of a database address instead of backup the entire studentdb database.

C:\mongodb\bin>mongorestore --db studentdb --collection address 

dump/studentdb/address.bson

This will restore only the selected collection of a database address instead of restore the entire studentdb database. 


Querying for Data

The method of fetching or getting data from a MongoDB database is carried out by using queries. While performing a query operation, one can also use criteria or conditions which can be used to retrieve specific data from the database?

Querying the Records using find()

MongoDB provides a function called db.collection.find() which is used for retrieval of documents from a MongoDB database.

The syntax for the read operation is as follows:

db.collection.find(filterprojection)

·       filter: It is an optional parameter. It specifies the selection filter with the help of query operators.

·       projection: It is an optional parameter. It specifies that only those fields return to the document that matches the given query filter. 

Here i cover different operations for querying the data from the database:

1. retrieve all documents in a collection

db.collection.find({}); 

2. retrieve documents in a collection using a condition ( similar to WHERE in MYSQL )

db.collection.find({key: value}); 

Example:

db.studentdb.find({email:"manish@email.com"}); 

3. Display all Records without mention any query.

db.collection.find({});

 

4. Display First Record Only

db.studetdb.findOne() 

            db.studetdb.find()[0]

5. Select documents that satisfy the given condition Using Query operators.

Syntax:                 

db.collection.find({field: {operator: value}})

In this section, we are going to briefly discuss the below conditional operators.

·       $eq: This operator fetch the documents from a collection that are equal to the given value expression

·     $gt: This operator fetch the documents from a collection that are greater than the given value expression

·       $gte: This operator fetch the documents from a collection that are greater than or equal to the given value expression

·       $lt: This operator fetch the documents from a collection that are less than the given value expression

·       $lte: This operator fetch the documents from a collection that are less than or equal to the given value expression

·       $in: This operator is used to get the documents where the value of a field equals any value in the specified array.

·       $nin: This operator is used to get the documents where the value of a field does not equals any value in the specified array. 

To learn how to handle the different comparison query operators provided by the Mongo database, let’s create a collection inventory in warehouse database.

use warehouse 

db.inventory.insertMany( [
    { _id: 101item: { name: "ab"code: "123" }, qty: 15tags: [ "A""B""C" ] },
    { _id: 102item: { name: "cd"code: "456" }, qty: 20tags: [ "B" ] },
    { _id: 103item: { name: "ij"code: "789" }, qty: 25tags: [ "A""B" ] },
    { _id: 104item: { name: "xy"code: "000" }, qty: 30tags: [ "B""A" ] },
    { _id: 105item: { name: "mn"code: "123" }, qty: 20tags: [ [ "A""B" ], "C" ] }
] ); 

Example-1. 

db.inventory.find( { qty: { $eq: 20 } } ) 

This command will only get those documents from the inventory collection where the value of the qty field equals the given value i.e. 20. 

 Example-2. 

db.inventory.find( { qty: { $lt: 20 } } )

This command will only get those documents from the inventory collection where the value of the qty field is less than the given value i.e. 20.

             

Example-3. 

db.inventory.find( { qty: { $in: [ 20, 25 ] } } )

This command will only get those documents from the inventory collection where the value of the qty field value is either 20 or 25.

             

Logical Query operators

·   $and: Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.

·       $not: Inverts the effect of a query expression and returns documents that do not match the query expression.

·       $or: Joins query clauses with a logical OR returns all documents that match the conditions of either clause.

·       $nor: Joins query clauses with a logical NOR returns all documents that fail to match both clauses.

Let’s understand this with the help of an example.

Example-1 

db.inventory.find( { $and: [ {"tags": "A"}, {"qty": 25} ] } )

This command will only get those documents from the inventory collection where the value of the tags, qty field matches the given values i.e. A and 25.

Example-2 

db.inventory.find( { $or: [ {"tags": "A"}, {"qty": 20} ] } )

This command will only get those documents from the inventory collection where the value of the tags, qty field matches the given values i.e. A and 20.

 

Projection the Data

In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.

MongoDB's find() method, explained in MongoDB Query Document accepts second optional parameter that is list of fields that you want to retrieve. In MongoDB, when you execute find() method, then it displays all fields of a document. To limit this, you need to set a list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the fields.

Syntax

The basic syntax of find() method with projection is as follows −

db.COLLECTION_NAME.find({},{KEY:1}) 

Example:

        db.inventory.find( {},{ _id:0item:1qty:1} )

This command will display only those fields in documents from the inventory collection where in the projection field’s value set to 1 and by default _id field will always display, so using projection filed set to 0 will also hide the _id filed in the ouput data. 

Limit the Records

To limit the records in MongoDB, you need to use limit() method. The method accepts one number type argument, which is the number of documents that you want to be displayed.

Syntax

The basic syntax of limit() method is as follows −

db.COLLECTION_NAME.find().limit(NUMBER)

Example

db.inventory.find().limit(2) 

This command will display only 2 documents from the inventory collection because limit function limiting the number of records to display.

 

Skip() Method: Apart from limit() method, there is one more method skip() which also accepts number type argument and is used to skip the number of documents.

Syntax

The basic syntax of skip() method is as follows −

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

Example

db.inventory.find().limit(3).skip(1) 

This command will skips the first document in the database and then display next 3 documents only.

 

Sort the Records

To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.

Syntax

The basic syntax of sort() method is as follows −

db.COLLECTION_NAME.find().sort({KEY:1})

Example

db.inventory.find().sort({qty:1}) 

This method will sort the document in ascending order based on qty key field.

 

db.inventory.find().sort({qty:-1}) 

This method will sort the document in descending order based on qty key field.

NOTE: If you do not specify the sorting preference(i.e 1 or -1), then by default documents in a collection are sorted in ascending order.


Tuesday, May 12, 2020

CRUD Operations

CRUD operations refer to the basic Insert, Read, Update, and Delete operations. In the previous chapter, we learned about how to create a database and drop the database in MongoDB. Now, let us learn how to perform CRUD (Create/Read/Update/Delete) operations in MongoDB.

MongoDB: Inserting a document into a collection (Create)

The command db.collection.insert() will perform an insert operation into a collection of a document.

Let us insert a document to a student collection. You must be connected to a database for doing any insert. It is done as follows:

db.studentdb.insert({

    regNo: "3014",

    name: "Manish Yadav",

    email: "manish@abc.com"

})

Note that an entry has been made into the collection called studentdb.

There’s another method called db.collection.insertMany() that lets you insert multiple documents at once. Here’s the syntax:

db.collection.insertMany([ <document 1> , <document 2>, ... ])

Let’s create a studentdb collection and populate it with some actual students:

 db.studentdb.insertMany([

    { regNo: "3014"name: "Manish Yadav"email: "manish@abc.com"},

    { regNo: "3015"name: "Sachin Kumar"email: "sachin@abc.com"},

    { regNo: "3016"name: "Gautam Gandhi"email: "gautam@abc.com"},

    { regNo: "3017"name: "sonal Sharma"email: "sonal@abc.com"},    

 ])

 

MongoDB: Querying a document from a collection(Read)

To retrieve (Select) the inserted document, run the below command. The find() command will retrieve all the documents of the given collection.

db.studentdb.find() 

"_id" : ObjectId("5ebab48b449902ce0ca86fc0")"regNo" : "3014""name" : "Test Student""course" : { "courseName" : "MCA""duration" : "3 Years" }, "address" : { "city" : "Bangalore""state" : "KA""country" : "India" } } 

NOTE: Please observe that the record retrieved contains an attribute called _id with some unique identifier value called ObjectId which acts as a document identifier.

If a record is to be retrieved based on some criteria, the find() method should be called passing parameters, then the record will be retrieved based on the attributes specified.

db.collection_name.find({"fieldname":"value"})

For Example: Let us retrieve the record from the student collection where the attribute regNo is 3014 and the query for the same is as shown below:

db.studentdb.find({"regNo":"3014"}).pretty()

The pretty() method specifies the cursor object to display the Mongo query results in an easy-to-read attractive format. 

 

MongoDB: Updating a document in a collection (Update)

In order to update specific field values of a collection in MongoDB, run the below query.

db.collection_name.update()

update() method specified above will take the fieldname and the new value as an argument to update a document.

Let us update the attribute name of the collection student for the document with regNo 3014.

db.studentdb.update({"regNo": "3014" },

{$set: { "name":"Manish Kumar Yadav"} } )

 


MongoDB: Removing an entry from the collection(Delete)

Let us now look into the deleting an entry from a collection. In order to delete an entry from a collection, run the command as shown below:

db.collection_name.deleteOne({"fieldname":"value"})

For Example: 

db.studentdb.deleteOne({"regNo":"3014"}) 

Note that after running the deleteOne() method, the entry has been deleted from the student collection.


You can perform all the above CRUD operations using following methods too in combinations: