MongoDB Presentaton
MongoDB Presentaton
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
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
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})