0% found this document useful (0 votes)
59 views12 pages

BDA - MongoDB

big data analytics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views12 pages

BDA - MongoDB

big data analytics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MONGO DB

 Non – relational database (NoSQL).


 It does not utilize the usual rows and columns unlike relational databases.
 It’s a schema-less database unlike RDBMS.
 Stores data in the form of JSON documents.
Differences between MongoDB and Relational Databases :
 MongoDB is a non-relational, document oriented database. RDBMS is a relational database.
 It is suitable for hierarchical data storage unlike RDBMS.
 MongoDB is horizontally scalable i.e we can add more servers. RDBMS is vertically scalable i.e
increasing RAM.
 MongoDB is document-based whereas RDBMS is row-based.
 MongoDB is field-based whereas RDBMS is column-based.
 MongoDB supports JSON query language along with SQL. RDBMS only supports SQL query
language.
 MongoDB has a dynamic schema. RDBMS has a predefined schema.
 MongoDB is almost a 100 times faster than RDBMS.
 MongoDB does not support complex joins whereas RDBMS supports complex joins.

 A group of documents make a collection and a group of collections make a database.


1) mongosh – used to connect to a MongoDB database

2) show dbs – To show all the available databases

3) use – To change or create a new database


 In MongoDB, a database is not actually created until it gets content.
 So, the command ‘show dbs’ does not list the database.

4) Creating a collection :
 createCollection() method - db.createCollection(“students”)
 insert method
- db.students.insertOne(*object*)
- creates a collection ‘students’ if it doesn’t exist and inserts the object.
5) Inserting documents :
 Inserting one document – db.students.insertOne({*object*})
 Inserting multiple documents – db.students.insertMany( [{*object*}, {*object*},{ }…] )

6) Displaying records :
 Syntax - db.students.find({*query*}, {*projection*})
- Query parameter :
 Used to filter data
- Projection parameter :
 Used to describe the fields to be included/returned.
 If left empty, all fields are included.
 Use ‘true’ to include a field and ‘false’ to exclude a field.
 Displaying all records – db.students.find()
 Displaying one record :
- db.students.findOne( { }, { } )
- If left empty, it returns the first document.

Inserting one document


Inserting multiple documents

Retrieving the name and cgpa of a student with a cgpa of 7.5

 The _id field is a default and is unique for every object.


 _id can be ignored by setting _id : false in the projection parameter.
 We cannot specify both true and false in the same object. The _id field is an
exception.
Retrieving records of students with an age of 19

7) Updating documents :
 Syntax – db.students.updateOne({*filter*}, {*update*}) OR
db.students.updateMany({ }, { })
- Filter parameter :
 Used to describe/filter out the documents to be updated.
- Update parameter :
 Used to define the updation.
 Updating one document :
- db.students.updateOne( { }, { } )
- updates the first document that is found matching the query.
 Updating multiple documents :
- db.students.updateMany( { }, { } )
 Updation methods :
- $set - to set a value to a field
- $unset – to remove a field
- $inc – to increment a field
- $rename – to rename a field
Updating the cgpa of john to be 9

 If a field doesn’t exist, the $set operator creates the field.

Creating an ‘internship’ field to all records and setting it to ‘no’


Incrementing the cgpa of max by 2

Removing the ‘internship’ field from all records


 upsert :
- Used to insert a document if not found.
- Syntax – db.students.updateOne( { }, {$set : { } }, {upsert : true})

Updating mike’s document but if not found, then inserting it


8) Deleting documents :
 Deleting one document – db.students.deleteOne( { *query*} )
 Deleting multiple documents – db.students.deleteMany( { *query*} )

Deleting a single record wherein the students’ cgpa is 9.5

Deleting all records of students of age 19

9) Deleting the database – db.dropDatabase()


MongoDB aggregations :
 Aggregation operations allow us to group, sort, limit, perform calculations, and much more.
 Aggregation pipelines have one or more stages.
 Each stage acts upon the result obtained from the previous stage. Hence, the order of the
stages are important.

1) $group :
- This stage groups documents by the unique _id parameter.
- Syntax – db.students.aggregate( [ {$group { _id : “ “ } } ] )
- NOTE : this _id is different from the unique ObjectId provided to each object.

Consider these to be the records in the students collection :

Returning the distinct values from the ‘age’ field


2) $limit :
- This stage limits the number of documents passed onto the next stage.

Returning 1 student record from the collection


3) $sort :
- This stage sorts all the documents in the specified order.
- Sort order is defined using either 1 or -1.
- 1 indicates ascending order and -1 indicates descending order.

Returning records of students in descending order of cgpa

4) $project :
- This stage passes only the specified fields onto the next stage.
- Similar to the projection parameter used with the find() method.
Returning only the name, age and cgpa of all students
5) $count :
- This stage counts the number of documents passed on by the previous stage.

Stages in the above :


- 1st stage : sorts the records in order of descending cgpa.
- 2nd stage : takes 1 record (the first record) out of all i.e the student with the highest
cgpa.
- 3rd stage : returns the number of records as a new field called ‘topper’. Here, it’s 1.

You might also like