0% found this document useful (0 votes)
57 views30 pages

MongoDB Presentaton

Mongo db ppts for lab

Uploaded by

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

MongoDB Presentaton

Mongo db ppts for lab

Uploaded by

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

MongoDB

Presented By:
Mr. Amarnath B Patil
Asst. Prof. CSE (Data Science)
Introduction
MongoDB is a open source , document-oriented NoSQL database
management system.
A document database(also known as a document-oriented database or
document store) is a database that stores information in document.
Designed for flexibility , Scalability, and performance in handling
structured and un-structured data
SQL V/s MongoDB
SQL NoSQL
SQL are relational database NoSQL Database are non-
relational database
They are structured table to store They provide flexibility in data
data in rows and columns storage, allowing varied data types
and structures.
Suitable for applications with well Ideal for application with
defined schemas and fixed data dynamic or evolving data models
structured
E-commerce platform, HR CMS, Social media platform,
management Gaming platform
Ex: MySQL, Oracle, PostgreSQL Ex: MongoDb, Cassandra, Redis
MongoDB Terminology
Key Features
How mongoDB works

Back End Wired Tiger


Frond End Database Or
MMAPV1

Read and Write


Data Files
JSON V/s BSON
In MongoDB, we write in JSON format only but behind the sence
the data is stored in BSON ( Binary JSON) format, a binary
representation on JSON.

By utilizing BSON , MongoDB can achieve higher read write


speeds, reduce storage requirements and improve data
manipulation capabilities , making it well suited for handling large
cand complex datasets while maintaining performance efficiency.
Document
JSON Document:
(
{"_id":0,"name":"aimee Zank","scores":
[{"score":1.463179736705023,"type":"exam"},
{"score":11.78273309957772,"type":"quiz"},
{"score":35.8740349954354,"type":"homework"}]}
)
Basic Queries
1. Show dbs;

2. Use database name;

3. db.dropDatabase();

4. Show collections;

5. Db.createCollection(‘Collection Name’);

6. Db.collectionname.drop();

7. Db.collectionname.find();
CRUD Operations
Insert One Insert many
Db.collectionname.insertOne({ Db.collectionname.insertOne([{
field1:value1, field1:value1,
field2:value2, field2:value2……},
…………. {
}) field1:value1,
field2:value2……},
//………..])
When to use quotes and when not
to use
• Special Character
• If a field name contains special character , starts with numeric or space, using
quote is necessary

• Reserved Character
• If field name is reserved keyword in MongoDB, use quotes distinguished it
from the reserved keywords
Ordered and Unordered insert
When executing the bulk operations, “Ordered and “Unordered” will
determine the batch behaviour
• Ordered insert:
Default behaviour is ordered where MongoDB stops on first error
db.collectionname.inserMany([{key:values},…..])
• Unordered inserts:
when executing bulk write operations with unordered flag MongoDB
processing after encountering the error
db.collectionname.inserMany([{key:values},…..],{ordered : false})
Case Sensitivity in MongoDB
• Collection names are case sensitive
• Field name within document are also case sensitive

db.Product.inserOne(name : ‘XYZ’, price : 123)


db.product.inserOne(name : ‘XYZ’, price : 123)
Read Operation
• find()
db.collectionName.find()->>>>>for all the data in that collection
db.collectionName.find({key : value})->>>>>> for specific set of data
as per the matching condition
db.collectionName.findOne({key : value})->>>>>>for specific One
data as per the matching condition
Import Json in MongoDb
Inside the array
Mongoimport jsonfile.json –d database_Name –c collection_name

Inside the array


Mongoimport jsonfile.json –d database_Name –c collection_name --
jsonArray
Export
Mongoexport –d document –c collection –o path of that file
Comparision Operator
• $eq: Equal to db.collectionName.find({fieldna
• $ne: not Equal to me:{$operator: Value}})
• $gt:Greater than • db.products.find({‘Price’:{$gte:
18000}})
• $lt: Less Than
• $gte: Greater than equal to
• db.products.find({ Price:{$in:
• $lte: Less than equal to [235,567,876]}})
• $in: in
• $nin: .count(), .limit()
Introduction to Cursors
Cursors in MongoDB are used to retrieve efficiently large result
sets from the queries, providing control over the data retrieval process.
• MongoDB retrieves results in Batches using cursors
• Cursors are pointer to the results in servers
• Cursors are used to iterate through query results
Automatic Batches:
• MongoDB retrieves query results in batches
• Default batch size is 101 documents
• This improves memory efficiency and network uses
Cursor Methods
1. count()- db.products.find({price : {$gt: 250}}).count()
2. limit()db.products.find({price : {$gt: 250}}).limit(5)
3. skip()db.products.find({price : {$gt: 250}}).skip()
4. sort()db.products.find({price : {$gt: 250}}).sort(1)
Note: 1 for Ascending and -1 for Descending
Cursor Methods
Performance Implication:
1. Skip() can be inefficient for large offsets
2. Using sort() on large result sets may impact performance

Use with caution


3. Be cautions when using limit() and skip() on large collection
4. Consider using indexing to optimize query performance.
Logical Operator
• $and : It performs a logical AND operation on an array of expression ,
where all expression must be true for the document to match

• $or : It performs a logical AND operation on an array of expression ,


atleast one expression must be true for the document to match
Logical Operator
1. $and • {$and : [{Condition1, condition2,
2. $or …..}]}
3. $nor
• {field: {$not : {operator : value}}}
4. $not

Example:
db.contributor.find({$and:
[{branch: "CSE"},

{joiningYear: 2018}]})
Complex Expression
• The $expr operator allows using aggregation expressions within a
query.
• Useful when you need to compare the fields from the same
documents in more complex manner
Syntax:
{$expr : {Operator: [field : value]}}
Example :
db.College_books.find({$expr : {$gt: [‘_id’ : 15]}} )
Elements Operator
• $exists : it checks wether that data is available or not
{field : {$exists : <boolean>}}
• $type : it search the document based on bson data type of a field
1.Double 2. String 3. Object 4. Array 5. Binary data 7. Object Id 8.
Boolean
• $size: Use to Embeded Documents
{field : {$size : <array lenght>}}
Projections
db.collection.find({‘field : value}, {field1: 1 , field2 : 0})
• To include specific fields , use projection with value of 1 for the field
you want
• To exclude specific fields , use projection with value of 0 for the field
you want
• You cannot include and exclude fields simultaneously in the same
query projection.
Example: db.mycol.find({},{‘title’=1,_id = 0})
Embedded Documents
• Document Inside the document is called Embedded Documents
• Dealing with Arrays and objects
• Just we need to use dot notation that’s it

Example Query:
db.students.find({'scores.score' : {$gt : 90}})
$all and $elemMatch
• The $all operator selects the documents where all the value of the field is an
array that contains all the specific fields
{field: {$all : [value1,value2,….] }}
db.students.find({‘scores.score’ : {$all : [value1,value2]}})

• The $elemMatch operator matches the documents that contains an array field
with atleast one element that matches all the specified query criteria
{field: {$elemMatch : {Query1,Query2,….} }}
db.students.find({‘scores: {$elemMatch : { ‘Score’ : value, ’type’: value]}})
Update
• UpdateOne()
• UpdateManye()
• Removing and renaming fields
UpdateOne and Updatemany
syntax
• Db.collection.updateOne(
{filter}, {$set : {existingfield : new value , newField : new
Value}})
db.students.updateOne( { _id: 9 }, { $set: { name : 'Amar'} })

• Db.collection.updateMany(
{filter}, {$set : {existingfield : new value //,}})
db.students.updateMany( { _id: {$lt: {_id : 3}} }, { $set: { name:
"Patil"}})
Removing and Renaming
• Db.collection.updateOne(
{filter}, {$unset : {existingfield : 1}})
• Db.collection.updateOne(
{filter}, {$rename : {existingfield : new value}})
Delete Operation
• DeleteOne()
db.document.deleteOne({key : value})
• DeleteMany()
db.document.deleteMany({key : value})

You might also like