Mongo DB Running Notes
Mongo DB Running Notes
MangoDB or MongoDB???
MongoDB
Humongous
https://www.mongodb.com/
1
1. MEAN Stack:
--------------
M --->MongoDB
E --->Express
A --->Angular
N --->Node JS
2. MERN Stack:
--------------
M --->MongoDB
E --->Express
R --->React JS
N --->Node JS
By using MEAN and MERN stacks we can build javascript based web
applications.
2
Relational Database vs Document Database:
-----------------------------------------
There are 2 most common types of databases.
eg: MongoDB
3
MongoDB Structure:
------------------
MongoDB Physical database contains several logical databases.
Each database contains several collections. Collection is something
like table in relational database.
Each collection contains several documents. Document is something
like record/row in relational database.
eg:
Database: Shopping cart database
Collections: Customers, Products, Orders
Cusomer Collection: contains several documents
document1:
{
"Name":"Sunny",
"age":40,
"Salary":10000
}
document2:
{
"Name":"Durga"
}
document-3:
{
"name":"Bunny",
"age":30,
"address":
{
"city":"Hyderabad"
},
4
"hobbies":[
{"name":"Cricket playing"},
{"name":"swimming"}
]
}
5
Note: Performance and Flexibility are biggest assets of Mongodb.
500 Rs
[email protected]
8885252627
8096969696
6 months videos
durgasoftonline.com
Title-Introduction to MongoDB
https://www.youtube.com/watch?v=wzrwN2wGJwU
C --->Create
R --->Retrieve
U --->Update
D --->Delete
6
To Launch/Start MongoDB Server --->mongod command
To Launch/Start MongoDB Shell --->mongo command
MongoDB Drivers:
----------------
From Application(Java,Python,C# etc) if we want to communicate with
database, some special software must be required, which is nothing
but Driver software.
mongodb.com--->Docs-->Drivers
https://pymongo.readthedocs.io/en/stable/tutorial.html
27017
7
Material
Running Notes
Videos
MongoDB Installation:
---------------------
https://www.mongodb.com/try/download/community
8
"environment": {
"distmod": "windows",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
> db.version()
4.4.2
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db,
and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless
aggregation on this database; returns a cursor
db.auth(username, password)
9
db.cloneDatabase(fromhost) - will only function with MongoDB 4.0
and below
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - will only function with
MongoDB 4.0 and below
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createUser(userDocument)
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.currentOp() displays currently executing operations in the db
db.dropDatabase(writeConcern)
db.dropUser(username)
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the
names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSecondaryOk() allow queries on a replication
secondary server
db.getName()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow
threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any
operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
10
db.isMaster() check replica primary status
db.hello() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSecondaryReplicationInfo()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is
a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setVerboseShell(flag) display extra information in shell output
db.setWriteConcern(<write concern doc>) - sets the write concern
for writes to the db
db.shutdownServer()
db.stats()
db.unsetWriteConcern(<write concern doc>) - unsets the write
concern for writes to the db
db.version() current version of the server
db.watch() - opens a change stream cursor for a database to
report on all changes to its non-system collections.
> db.stats()
{
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
11
"dataSize" : 0,
"storageSize" : 0,
"totalSize" : 0,
"indexes" : 0,
"indexSize" : 0,
"scaleFactor" : 1,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1
}
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin
switched to db admin
> show collections
system.version
> use local
switched to db local
> show collections
startup_log
mongodb--->install-->Physical database
estoredb--->
collegedb-->
12
data/db everytime
500 Rs
Running Notes
Material PDFs
Videos 6 months access (durgasoftonline.com)
Colorful Course Completion Certificate
Session-1: https://www.youtube.com/watch?v=wzrwN2wGJwU
Session-2: https://www.youtube.com/watch?v=Ncz7h80IzC4
Default Databases:
------------------
MongoDB Admin will use these default databases.
1. admin:
---------
13
admin database is used to store user authentication and authorization
information like usernames,passwords,roles etc
This database is used by administrators while creating,deleting and
updating users and while assigning roles.
2. config:
----------
To store configuration information of mongodb server.
3. local:
---------
local database can be used by admin while performing replication
process.
14
JSON-->10KB
BSON--->4 to 5 KB
Note: Efficient Storage and Extra data types are speciality of BSON
over JSON.
EJSON--->Extended JSON
At the time of retrieval BSON data will be converted to EJSON for
understanding purpose.
15
admin 0.000GB
config 0.000GB
local 0.000GB
> use durgadb
switched to db durgadb
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
16
Because we already switched to durgadb because of "use durgadb"
command
db.collection.drop()
db.students.drop()
17
local 0.000GB
Sir, from anywhere like from another database can we drop other
database??
db.collection.insertOne()
db.collection.insertMany()
db.collection.insert()
db.employees.insertOne({eno:100,ename:"Sunny",esal:1000,eaddr:"Hy
d"})
eg: db.employees.find()
> db.employees.find()
{ "_id" : ObjectId("5fe16d547789dad6d1278927"), "eno" : 100, "ename"
: "Sunny", "esal" : 1000, "eaddr" : "Hyd" }
18
{ "_id" : ObjectId("5fe16da07789dad6d1278928"), "eno" : 200, "ename"
: "Bunny", "esal" : 2000, "eaddr" : "Mumbai" }
{ "_id" : ObjectId("5fe16dc67789dad6d1278929"), "eno" : 300, "ename"
: "Chinny", "esal" : 3000, "eaddr" : "Chennai" }
{ "_id" : ObjectId("5fe16ddb7789dad6d127892a"), "eno" : 400, "ename"
: "Vinny", "esal" : 4000, "eaddr" : "Delhi" }
> db.employees.find().pretty()
{
"_id" : ObjectId("5fe16d547789dad6d1278927"),
"eno" : 100,
"ename" : "Sunny",
"esal" : 1000,
"eaddr" : "Hyd"
}
{
"_id" : ObjectId("5fe16da07789dad6d1278928"),
"eno" : 200,
"ename" : "Bunny",
"esal" : 2000,
"eaddr" : "Mumbai"
}
{
"_id" : ObjectId("5fe16dc67789dad6d1278929"),
"eno" : 300,
"ename" : "Chinny",
"esal" : 3000,
"eaddr" : "Chennai"
}
{
"_id" : ObjectId("5fe16ddb7789dad6d127892a"),
"eno" : 400,
"ename" : "Vinny",
19
"esal" : 4000,
"eaddr" : "Delhi"
}
3. U-->Update Operation:
------------------------
db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()
20
{
"_id" : ObjectId("5fe16d547789dad6d1278927"),
"eno" : 100,
"ename" : "Sunny",
"esal" : 1000,
"eaddr" : "Hyd"
}
{
"_id" : ObjectId("5fe16da07789dad6d1278928"),
"eno" : 200,
"ename" : "Bunny",
"esal" : 2000,
"eaddr" : "Mumbai"
}
{
"_id" : ObjectId("5fe16dc67789dad6d1278929"),
"eno" : 300,
"ename" : "Chinny",
"esal" : 3000,
"eaddr" : "Chennai"
}
{
"_id" : ObjectId("5fe16ddb7789dad6d127892a"),
"eno" : 400,
"ename" : "Vinny",
"esal" : 10000,
"eaddr" : "Delhi"
}
Note: If anything prefixed with $ symbol, then it is predefined word in
MongoDB.
4. D -->Delete:
---------------
21
db.collection.deleteOne()
db.collection.deleteMany()
db.employees.deleteOne({ename:"Vinny"})
Session-1: https://www.youtube.com/watch?v=wzrwN2wGJwU
22
Session-2: https://www.youtube.com/watch?v=Ncz7h80IzC4
CRUD
Capped Collections:
------------------
> use durgadb
> db.createCollection("employees")
db.createCollection(name)
db.createCollection(name,options)
capped
max 1000 documents--->1001 document
size: 3736578 bytes only-->if space completed
23
3. db.createCollection("employees",{capped: true, size: 365675})---
>valid
4. db.createCollection("employees",{size: 365675})--->invalid
"errmsg" : "the 'capped' field needs to be true when either the 'size'
or 'max' fields are present"
use case:
---------
freshers jobs portal--->students registered
students collection---> 1 lakh
CRUD
Inserting Documents in the collection(C--->Create):
--------------------------------------------------
db.collection.insertOne()
db.collection.insertMany()
db.collection.insert()
db.collection.insertOne():
24
-------------------------
To insert only one document.
db.employees.insertOne({...})
Argument is only one javascript object.
db.collection.insertMany():
--------------------------
To insert multiple documents
db.collection.insertMany([{..},{..},{..},{..}])
db.collection.insert():
-----------------------
To insert either a single document or multiple documents.
db.employees.insert({...})
db.employees.insert([{..},{..},{..},{..}])
25
db.employees.insert({eno: 700, ename: "Sunny", esal: 1000, eaddr:
"Mumbai"})
db.employees.insertOne(emp)
db.employees.insertMany([emp])
db.employees.insert(emp)
db.employees.insert([emp])
_id
26
insertOne() vs insertMany() vs insert()
How to create document before insertion
bit.ly/durgamongodb
students.js:
------------
db.students.insertOne({name: "Durga", rollno: 101, marks: 98 })
db.students.insertOne({name: "Ravi", rollno: 102, marks: 99 })
db.students.insertOne({name: "Shiva", rollno: 103, marks: 100 })
db.students.insertOne({name: "Pavan", rollno: 104, marks: 80 })
load("D:\students.js")
27
"rollno" : 101,
"marks" : 98
}
{
"_id" : ObjectId("5fe40341941e89a2bcd9f34c"),
"name" : "Ravi",
"rollno" : 102,
"marks" : 99
}
{
"_id" : ObjectId("5fe40341941e89a2bcd9f34d"),
"name" : "Shiva",
"rollno" : 103,
"marks" : 100
}
{
"_id" : ObjectId("5fe40341941e89a2bcd9f34e"),
"name" : "Pavan",
"rollno" : 104,
"marks" : 80
}
28
In javascript object, quote symbols for keys are optional.
But in JSON, quote symbols are mandatory for keys.
db.collection.insertOne(javascript object)
Here quote symbols are optional
students.json:
--------------
[
{
"name": "Sunny",
"rollno": 666
},
{
"name": "Bunny",
"rollno": 777
},
{
"name": "Chinny",
"rollno": 888
},
{
"name": "Vinny",
"rollno": 999
},
{
"name": "Pinny",
"rollno": 555
}
]
29
mongoimport --->tool to import documents from json file into MongoDB
https://www.mongodb.com/try/download/database-tools
****
Note: mongoimport command should be executed from the command
prompt but not from the shell.
30
mongoimport creates database and collection automatically if not
available.
If collection already available then the new documents will be
appended.
31
}
{
"_id" : ObjectId("5fe40aa643e59978520a102d"),
"name" : "Chinny",
"rollno" : 888
}
{
"_id" : ObjectId("5fe40aa643e59978520a102e"),
"name" : "Pinny",
"rollno" : 555
}
{
"_id" : ObjectId("5fe40aa643e59978520a102f"),
"name" : "Sunny",
"rollno" : 666
}
students.json:
--------------
[
{
"name": "Dhoni",
"rollno": 7777777
}
]
32
Note: Similarly, we can use mongoimport to import data from excel/csv
files.
can i import from anyother RDBMS like oracle? Yes
bit.ly/durgamongodb
sir please with python can you show the mysql to mongo program
Nested Documents:
-----------------
Sometimes we can take a document inside another document, such
type of documents are called nested documents or embedded
documents.
employees:
{
eno:100,
ename:"durga",
esal:1000,
eaddr:"Hyderabad",
hobbies: {h1:"Swimming",h2:"Reading"}
storedb-->database name
33
books--->collection
books.json
----------
[
{
"title": "Python In Simple Way",
"isbn": 12345,
"downloadable": true,
"no_of_reviews": 10,
"author": {
"name": "Daniel Kohen",
"callname": "Dan"
}
},
{
"title": "MongoDB In Simple Way",
"isbn": 45678,
"downloadable": false,
"no_of_reviews": 5,
"author": {
"name": "Shiva Ramachandran",
"callname": "Shiva"
}
}
]
34
{
"title": "Linux In Simple Way",
"isbn": 778899,
"downloadable": true,
"no_of_reviews":0,
"author": {
"name": "Shiva Ramachandran",
"callname": "Shiva",
"profile": {
"exp":8,
"courses":3,
"books":2
}
}
}
]
Note:
Inside Nested document, we can take another document also.
MongoDB supports upto 100 levels of nesting.
sir im confusing with server shell cmd which commads where we need
use
1. command name and purpose
db.collection.find().pretty()
db.collection.insertOne()
db.collection.insertMany()
db.collection.insert()
db.collection.updateOne({},{})
35
Arrays in Documents:
--------------------
Any collection of items is called an array.
The items can be strings or numbers or objects.
books.json:
-----------
[
{
"title": "Devops In Simple Way",
"isbn": 112233,
"downloadable": false,
"no_of_reviews":20,
"tags":["jenkins","git","CICD"],
"languages":["english","hindi","telugu"],
"author": {
"name": "Martin Kohenova",
"callname": "Mart",
"profile": {
"exp":8,
"courses":3,
"books":2
}
}
}
]
36
sir we use find() to fetch all documents but how to fetch a particular
field in the document ?
ObjectId:
---------
For every document, MongoDB Server will associate a unique id, which
is nothing but ObjectId.
"_id" : ObjectId("5fe6ad34b195d71b16a713c8")
ObjectId is of 12 bytes.
1. The first 4 bytes represents the timestamp when this document was
inserted.
2. The next 3 bytes represents machine identifier( host name)
3. The next 2 bytes represents process id.
4. The last 3 bytes represents some random increment value.
37
mobile number contains 10 digits
why 10 digits, just only one digit is enough???
> db.employees.find()[0]
{
"_id" : ObjectId("5fe2b6fc9d0c84a052cb9745"),
"eno" : 100,
"ename" : "Sunny",
"esal" : 1000,
"eaddr" : "Mumbai"
}
> db.employees.find()[0]._id
ObjectId("5fe2b6fc9d0c84a052cb9745")
> db.employees.find()[0]._id.getTimestamp()
ISODate("2020-12-23T03:18:20Z")
38
ObjectId("5fe6b3218c25aae60be989c0")
> db.employees.find({"B":200})[0]._id.getTimestamp()
ISODate("2020-12-26T03:50:57Z")
By using _id field, we can provide our own value as ObjectId. MongoDB
server will generate default ObjectId iff we are not providing any _id
field value.
db.employees.insertOne({_id:789, name:"Rahul"})
db.employees.insertOne({_id:789, name:"Viraj"})
39
"errmsg" : "Performing an update on the path '_id' would modify the
immutable field '_id'",
Ans: K
Sir I have a querry. When I get stuck where to refer? You said you lernt
now only. Can u guide us?
40
How to Learn New Technologies very Easily?
https://www.youtube.com/watch?v=AQRdnqVh2NA
youtube.com/durgasoftware
bit.ly/durgamongodb
durgasoftonline.com
insertOne()
insertMany()
insert()
Ordered Insertion
WriteConcern
Atomiticity
41
Ordered Insertion in Bulk inserts:
-----------------------------------
We can perform bulk inserts either by using insertMany() or insert()
methods.
All documents present inside given array will be inserted into
collection.
durgadb database
alphabets collection
some documents-->bulk insert
insert([{},{},{},{},{}])
db.alphabets.insertMany([{A:"Apple"},{B:"Banana"},{C:"Cat"}])
> db.alphabets.find()
{ "_id" : ObjectId("5fe7f8998a9854ae87538e18"), "A" : "Apple" }
{ "_id" : ObjectId("5fe7f8998a9854ae87538e19"), "B" : "Banana" }
{ "_id" : ObjectId("5fe7f8998a9854ae87538e1a"), "C" : "Cat" }
42
The documents which are already inserted won't be rollbacked.
0
1
2
db.cars.find().pretty()
43
"nRemoved" : 0,
"upserted" : [ ]
}) :
BulkWriteError({
"writeErrors" : [
{
"index" : 1,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection:
durgadb.cars index: _id_ dup key: { _id: 100.0 }",
"op" : {
"_id" : 100,
"A" : "Audi"
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 1,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
BulkWriteError@src/mongo/shell/bulk_api.js:367:48
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:332:24
Bulk/this.execute@src/mongo/shell/bulk_api.js:1186:23
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:326:5
@(shell):1:1
> db.cars.find().pretty()
{ "_id" : 100, "M" : "Maruti" }
44
While performing bulk insert operation, if any document insertion fails
then rest of the documents won't be inserted. i.e in bulk inserts, order
is important.
The documents which are already inserted won't be rollbacked.
45
Transfer 10k from my account to sunny account
operation-1: debit 10k from my account
operation-2: credit 10k to sunny account
By default: No
But we can customize this behaviour by using ordered property.
Ans: 1
Ans: 1
46
Ans: 2
WriteConcern Property:
----------------------
usecase-1:
----------
Whenever we are performing insert operation, bydefault the
shell/client will wait until getting acknowledgement. Server will
provide acknowledgement after completing insert operation. This may
reduce performance at client side.
> db.cars.insertOne({I:"Innova"})
{
"acknowledged" : true,
"insertedId" : ObjectId("5fe800f68a9854ae87538e1b")
}
47
Even "acknowledged" : false, still document inserted.
> db.cars.find()
{ "_id" : ObjectId("5fe800f68a9854ae87538e1b"), "I" : "Innova" }
{ "_id" : ObjectId("5fe802f48a9854ae87538e1c"), "B" : "BMW" }
{ "_id" : ObjectId("5fe803248a9854ae87538e1d"), "H" : "Honda" }
usecase-2:
----------
In Production , for every database we have to maintain cloned/replica
database because
Diagram
if w: 0 ===> No acknowledgement.
if w: 1 ===> Acknowledgement after inserting document in primary
database.
48
if w: 2 ===> Acknowledgement after inserting document in primary
database and replica-1.
if w: 3 ===> Acknowledgement after inserting document in primary
database, replica-1 and replica-2.
db.cars.insertOne({A:"Audi"},{writeConcern: {w: 3} })
> db.cars.insertOne({A:"Audi"},{writeConcern: {w: 3} })
uncaught exception: WriteCommandError({
"ok" : 0,
"errmsg" : "cannot use 'w' > 1 when a host is not replicated",
"code" : 2,
"codeName" : "BadValue"
})
49
12345,yvan,15000,noida
50
MongoDB Server stores either complete document or nothing. ie it
won't store part of the document. ie CRUD operations are atomic at
document level.
db.collection.insertMany([{},{},{},{}])
But while inserting multiple documents (Bulk Insertion), after inserting
some documents if database server faces some problem, then already
inserted documents won't be rollbacked. i.e atomicity bydefault not
applicable for bulk inserts.
CRUD Operations-->C
51
These methods are related to collection and hence we have to call
these methods on collection object.
db.collection.find()
db.collection.findOne()
eg:
read all employees
read all employees where esal > 10000
read all employees where eaddr is Hyderabad
read all employees where eaddr is Hyderabad or esal > 10000
read all employees where eaddr is Hyderabad and esal > 10000
aggregate functions
logical operations
etc
52
"languages": ["english","hindi","telugu"],
"author": {
"name": "Shiva Ramachandran",
"callname": "Shiv",
"profile": {
"exp":8,
"courses":3,
"books":2
}
}
},
{
"title": "Java in simple way",
"isbn": 1122,
"downloadable": true,
"no_of_reviews": 2,
"tags": ["language","freeware","programming"],
"languages": ["english","hindi","telugu"],
"author": {
"name": "Karhik Ramachandran",
"callname": "Karthik",
"profile": {
"exp":1,
"courses":2,
"books":3
}
}
},
{
"title": "Python in simple way",
"isbn": 1234,
"downloadable": false,
"no_of_reviews": 5,
53
"tags": ["language","freeware","programming"],
"languages": ["english","hindi","telugu"],
"author": {
"name": "Daniel IA Cohen",
"callname": "Dan",
"profile": {
"exp":8,
"courses":7,
"books":6
}
}
},
{
"title": "Devops in simple way",
"isbn": 6677,
"downloadable": false,
"no_of_reviews": 3,
"tags": ["jenkins","git","cicd"],
"languages": ["english","hindi","telugu"],
"author": {
"name": "Dhoni Chandra",
"callname": "Dhoni",
"profile": {
"exp":4,
"courses":4,
"books":4
}
}
},
{
"title": "MongoDB in simple way",
"isbn": 6677,
"downloadable": true,
54
"no_of_reviews": 4,
"tags": ["database","cloud","nosql"],
"languages": ["english","hindi","telugu"],
"author": {
"name": "Sachin Tendulkar",
"callname": "Sachin",
"profile": {
"exp":6,
"courses":7,
"books":8
}
}
},
{
"title": "Oracle in simple way",
"isbn": 6677,
"downloadable": true,
"no_of_reviews": 3,
"tags": ["database","sql","relational"],
"languages": ["english","hindi","telugu"],
"author": {
"name": "Virat Kohli",
"callname": "kohli",
"profile": {
"exp":2,
"courses":2,
"books":2
}
}
},
{
"title": "Shell Scripting in simple way",
"isbn": 9988,
55
"downloadable": true,
"no_of_reviews": 1,
"tags": ["programming"],
"languages": ["english","hindi","tamil"],
"author": {
"name": "Rama Ramachandran",
"callname": "Rama",
"profile": {
"exp":8,
"courses":3,
"books":2
}
}
}
]
Note: pretty() and count() methods can be used on find() result but not
on findOne() result.
56
> db.books.findOne().pretty()
uncaught exception: TypeError: db.books.findOne(...).pretty is not a
function :
@(shell):1:1
> db.books.findOne().count()
uncaught exception: TypeError: db.books.findOne(...).count is not a
function :
Q5. List out all documents from books collection where no_of_reviews
is 3.
> db.books.find({no_of_reviews: 3}).pretty()
Q1. List out all documents from books collection where author's call
name is kohli?
Q2. List out all documents from books collection where author's profile
contains exactly 2 courses?
57
> db.books.find({"author.profile.courses": 2}).pretty()
Q1. List out all documents where 'tags' array contains 'programming'
element?
Q2. List out all documents where 'languages' array contains 'telugu'
element?
58
db.books.find({tags: ["language","programming","freeware"]}).pretty()
It won't return any document because the given order not matched
with any document.
Note:
> db.books.find({tags: "programming"}).pretty() ===>Query array
elements
> db.books.find({tags: ["programming"]}).pretty() ===>Query array itself
It is somthing like we are finding fruit in a basket not basket but in the
second case we are trying to find basket itself.
Query Operators:
----------------
We can use operators in our queries.
Every operator prefixed with $ symbol to indicate that it is operator
butnot field or value. By seeing $ symbol, MongoDB server will execute
the corresponding operator functionality.
59
Syntax: db.collection.find({ field: {$eq: value} })
It is exactly same as
db.collection.find({field: value}) ==>It is the short cut way
60
Q1. Select all documents from books collection where tags array is
exactly equal to ["language","freeware","programming"].
$ne operator:
-------------
ne ---> means not equal
We can use $ne operator to select all the documents where the value
of the field is not equal to specified value.
Note: If the specified field not available, such documents also will be
included in the result.
$gt operator:
-------------
$gt ---> means greater than
We can use $gt operator to select all documents where the value of
field is greater than specified value.
61
Syntax: db.collection.find({field: {$gt: value}})
$gte operator:
--------------
$gte ----> means greater than or equal to
We can use $gte operator to select all documents where the value of
field is greater than or equal to specified value.
$lt operator:
-------------
$lt ---> means less than
We can use $lt operator to select all documents where the value of
field is less than specified value.
62
> db.books.find({no_of_reviews: {$lt: 3}}).pretty()
$lte operator:
-------------
$lte ---> means less than or equal to
We can use $lte operator to select all documents where the value of
field is less than or equal to specified value.
$in operator:
-------------
We can use $in operator to select all documents where the value of a
field equals any value in the specified array.
It is something like python membership operator.
Q1. Select all documents from the books collection where the
no_of_reviews is 1 or 4 or 5?
63
Q2. Select all documents from the books collection where the tags
array contains either database or programming.
$nin operator:
---------------
$nin means not in
It is inverse of in operator
Note:
$in result + $nin result = total no of documents
> db.books.find({no_of_reviews: {$nin: [1, 4, 5]}}).count()
4
64
> db.books.find({no_of_reviews: {$in: [1, 4, 5]}}).count()
4
> db.books.find().count()
8
oldtamirindrecepie
If you are not having goal in your life, you will part of some other's
goal.
One Movie Dialogue sir ,If we not working for our goals then others will
use to full fill their Dreamzs.
$or operator:
-------------
65
$or performs logical OR operation on an array of two or more
expressions(conditions) and selects the documents that satisfy
atleast one of the expression(condition)
Q1. Select all documents where either no_of_reviews >3 or tags array
contains programming element?
$nor operator:
--------------
It is inverse of $or operator.
66
$or --->Atleast one condition satisfied
$nor --->neither condition satisfied i.e all conditions fails
eg:
c1: { no_of_reviews: {$gt: 3}}
c2: { downloadable: true}
67
--------------------------------------------------------------
$and operator:
--------------
$and performs logical AND operation on an array of one or more
expressions and selects the documents that satisfy all expressions in
the array. i.e all conditions should be satisfied.
Assignment:
students.json:
--------------
[
{
"name": "A",
"marks": 10
},
{
"name": "B",
"marks": 20
},
{
"name": "C",
"marks": 30
68
},
{
"name": "D",
"marks": 40
},
{
"name": "E",
"marks": 50
},
{
"name": "F",
"marks": 60
},
{
"name": "G",
"marks": 70
},
{
"name": "H",
"marks": 80
},
{
"name": "I",
"marks": 90
},
{
"name": "J",
"marks": 100
}
]
mongoimport --db storedb --collection students --file students.json --
jsonArray
69
Q1. Select all students where marks are less than 85 and greater than
45?
c1: {marks: {$lt: 85}}
c2: {marks: {$gt: 45}}
> db.students.find({$and: [{marks: {$lt: 85}}, {marks: {$gt: 45}}]}).pretty()
Q2. Select all students where marks are less than 50 and greater than
or equal to 35?
c1: {marks: {$lt: 50}}
c2: {marks: {$gte: 35}}
> db.students.find({$and: [{marks: {$lt: 50}}, {marks: {$gte:
35}}]}).pretty()
shortcut way:
> db.books.find({no_of_reviews: {$gte: 3}, downloadable: true}).pretty()
70
Limitation of this shortcut:
----------------------------
If the conditions are on the same field then this short cut won't work.
Q. List out all students whose marks are >=50 and <= 90?
71
{marks: {$lte: 90}}
-------------------------
Q. What is the difference between these two quires?
$not operator:
--------------
It is just to perform inverse operation.
72
$not operation performs logical NOT operation on the specified
operator expression and selects the documents that do not match
operator expression. This includes the documents that do not contain
the field.
eg:
c1: { no_of_reviews: {$gt: 3}}
1. $exists:
-----------
Syntax: {field: {$exists: <boolean>}}
73
Q2. Select all documents which does not contain no_of_reviews field.
case study:
-----------
db.students.insertOne({name: "Durga", marks: 100, gf: "Sunny"})
db.students.insertOne({name: "Ravi", marks: 20, gf: "Mallika"})
Q3. Select all students who are having the gf, but still marks are
greater than 70?
74
This operator is very helpful, whenever we are dealing with large
volumes of unstructured data where types are unpredictable.
Table:
BSON Type-------->Number------------>alias
==========================================
Double-------->1------------>"double"
String-------->2------------>"string"
Object-------->3------------>"object"
Array-------- >4------------>"array"
BinaryData---->5------------>"binData"
ObjectId------>7------------>"objectId"
Boolean------->8------------>"bool"
Date---------->9------------>"date"
Null--------->10------------>"null"
32 Bit Integer--------->16------------>"int"
75
64 Bit Integer--------->18------------>"long"
Decimal128--------->19------------>"decimal"
Note:
$type supports "number" alias, which will match the following BSON
Types.
int
long
double
decimal
Case Study:
-----------
db.phonebook.insertOne({_id: 1, name: "Sunny", phoneNumber:
"9292929292"})
db.phonebook.insertOne({_id: 2, name: "Bunny", phoneNumber:
8896979797})
db.phonebook.insertOne({_id: 3, name: "Chinny", phoneNumber:
NumberLong(9898989898) })
db.phonebook.insertOne({_id: 4, name: "Vinny", phoneNumber:
NumberInt(9246212143)})
db.phonebook.insertOne({_id: 5, name: "Pinny", phoneNumber:
["8885252627", 8096969696]})
76
Every number is bydefault treated as double type in MongoDB.
77
"name" : "Pinny",
"phoneNumber" : [
"8885252627",
8096969696
]
}
78
Q5. Select all documents where phoneNumber value is of number
type?
> db.phonebook.find({phoneNumber: {$type: "number"}}).pretty()
79
8096969696
]
}
1. $expr operator:
------------------
expr means expression.
Evaluate expression and select documents which satisfy that
expression.
Syntax:
{ $expr: {<expression>}}
Case Study: Compare two field values from the same document:
-----------------------------------------------------------
80
db.homeBudget.insertOne({category:"rent", budget:1500, spent:1500})
db.homeBudget.insertOne({category:"education", budget:2000,
spent:1000})
db.homeBudget.insertOne({category:"clothes", budget:750,
spent:1500})
db.homeBudget.insertOne({category:"entertinement", budget:1000,
spent:2500})
81
"spent" : 2500
}
Q2. Select all documents where spent amount is less than or equal to
budget amount?
82
2. $regex operator:
-------------------
regex means regular expression.
Syntax:
-------
We can use $regex operator in any of the following styles:
Case Study:
-----------
db.homeBudget.insertOne({category:"home food", budget:1000,
spent:1500})
db.homeBudget.insertOne({category:"outside food", budget:1000,
spent:2000})
db.homeBudget.insertOne({category:"rent", budget:1500, spent:1500})
db.homeBudget.insertOne({category:"education", budget:2000,
spent:1000})
db.homeBudget.insertOne({category:"clothes", budget:750,
spent:1500})
db.homeBudget.insertOne({category:"entertinement", budget:1000,
spent:2500})
83
> db.homeBudget.find({ category: { $regex: /food/}}).pretty()
> db.homeBudget.find({ category: { $regex: 'food'}}).pretty()
> db.homeBudget.find({ category: /food/}).pretty()
{
"_id" : ObjectId("5ff52c2345f8edf724d263ec"),
"category" : "home food",
"budget" : 1000,
"spent" : 1500
}
{
"_id" : ObjectId("5ff52c2345f8edf724d263ed"),
"category" : "outside food",
"budget" : 1000,
"spent" : 2000
}
Q2. Select all documents where category value starts with 'e'?
84
{
"_id" : ObjectId("5ff52c2645f8edf724d263f1"),
"category" : "entertinement",
"budget" : 1000,
"spent" : 2500
}
Note:
[abc] --->either a or b or c
[ec] --->either e or c
^[ec] ---> starts with either e or c
Q3. Select all documents where category value starts with either 'e' or
'c'?
85
"budget" : 1000,
"spent" : 2500
}
Note:
^t ---> starts with t
t$ ---> ends with t
Q4. Select all documents where category value ends with 't'?
> db.homeBudget.find({ category: { $regex: /t$/}}).pretty()
> db.homeBudget.find({ category: { $regex: 't$'}}).pretty()
> db.homeBudget.find({ category: /t$/}).pretty()
{
"_id" : ObjectId("5ff52c2345f8edf724d263ee"),
"category" : "rent",
"budget" : 1500,
"spent" : 1500
}
{
"_id" : ObjectId("5ff52c2645f8edf724d263f1"),
"category" : "entertinement",
"budget" : 1000,
"spent" : 2500
}
Q. Select all documents where category value ends with either 't' or
'n'?
> db.homeBudget.find({ category: { $regex: /[tn]$/}}).pretty()
> db.homeBudget.find({ category: { $regex: '[tn]$'}}).pretty()
> db.homeBudget.find({ category: /[tn]$/}).pretty()
{
86
"_id" : ObjectId("5ff52c2345f8edf724d263ee"),
"category" : "rent",
"budget" : 1500,
"spent" : 1500
}
{
"_id" : ObjectId("5ff52c2345f8edf724d263ef"),
"category" : "education",
"budget" : 2000,
"spent" : 1000
}
{
"_id" : ObjectId("5ff52c2645f8edf724d263f1"),
"category" : "entertinement",
"budget" : 1000,
"spent" : 2500
}
87
"category" : "education",
"budget" : 2000,
"spent" : 1000
}
{
"_id" : ObjectId("5ff52c2645f8edf724d263f1"),
"category" : "entertinement",
"budget" : 1000,
"spent" : 2500
}
3. $mod operator:
-----------------
mod means modulo operator or remainder operator.
It is very helpful to select documents based on modulo operation.
We can use $mod operator to select documents where the value of the
field divided by a divisor has a specified remainder.
Case Study:
----------
db.shop.insertOne({_id: 1, item: "soaps", quantity: 13})
db.shop.insertOne({_id: 2, item: "books", quantity: 10})
db.shop.insertOne({_id: 3, item: "pens", quantity: 15})
db.shop.insertOne({_id: 4, item: "pencils", quantity: 17})
88
{ "_id" : 3, "item" : "pens", "quantity" : 15 }
eg1:
> db.shop.find({ quantity: {$mod: [4]}}).pretty()
Error: error: {
"ok" : 0,
"errmsg" : "malformed mod, not enough elements",
"code" : 2,
"codeName" : "BadValue"
}
eg2:
> db.shop.find({ quantity: {$mod: [4,1,2]}}).pretty()
Error: error: {
"ok" : 0,
"errmsg" : "malformed mod, too many elements",
"code" : 2,
"codeName" : "BadValue"
}
4. $jsonSchema:
89
---------------
We can use this operator to select documents based on given
jsonSchema.
5. $text:
--------
It is related to indexes concept, will be discussed soon.
6. $where:
---------
It is deprecated and replaced $expr.
1. $all operator:
-----------------
We can use $all operator to select documents where array contains all
specified elements.
Syntax:
-------
{ field: { $all: [value1, value2, value3,...]}}
Case Study:
----------
db.courses.insertOne({_id:1,
name:"java",tags:["language","programming","easy","ocean"]})
90
db.courses.insertOne({_id:2,
name:"python",tags:["language","programming","easy"]})
db.courses.insertOne({_id:3,
name:"C",tags:["language","performance"]})
db.courses.insertOne({_id:4,
name:"Oracle",tags:["database","sql","cloud"]})
db.courses.insertOne({_id:5,
name:"MongoDB",tags:["database","nosql","cloud"]})
db.courses.insertOne({_id:6, name:"Devops",tags:["culture"]})
Q1. Select all documents where tags array contains "database" and
"cloud" elements?
91
Note: Order of elements is not important and it is not exact match.
> db.courses.find({tags: ["database","cloud"]}).pretty()===>Here order
is important and Exact Match
Q2. Select all documents where tags array contains "language" and
"programming" elements?
2. $elemMatch Operator:
92
-----------------------
elemMatch means element Match.
Syntax:
{field: {$elemMatch: {<query1>,<query2>,<query3>,...}}}
Case Study:
-----------
db.students.insertOne({_id:1,name:"Durga",marks:[82,35,99]})
db.students.insertOne({_id:2,name:"Ravi",marks:[75,90,95]})
Q1. Select documents where student has atleast one subject marks
greater than or equal to 80 but less than 90?
3. $size operator:
------------------
We can use $size operator to select documents based on specified
array size.
Case Study:
----------
93
db.courses.insertOne({_id:1,
name:"java",tags:["language","programming","easy","ocean"]})
db.courses.insertOne({_id:2,
name:"python",tags:["language","programming","easy"]})
db.courses.insertOne({_id:3,
name:"C",tags:["language","performance"]})
db.courses.insertOne({_id:4,
name:"Oracle",tags:["database","sql","cloud"]})
db.courses.insertOne({_id:5,
name:"MongoDB",tags:["database","nosql","cloud"]})
db.courses.insertOne({_id:6, name:"Devops",tags:["culture"]})
94
"language",
"programming",
"easy"
]
}
{
"_id" : 4,
"name" : "Oracle",
"tags" : [
"database",
"sql",
"cloud"
]
}
{
"_id" : 5,
"name" : "MongoDB",
"tags" : [
"database",
"nosql",
"cloud"
]
}
95
--------------------------------------------
vidya.csv:
----------
empno name salary
1 Vidya 5000
2 Ravi 2000
3 Durga 6000
4 Sushma 3000
96
}
{
"_id" : ObjectId("5ff7cfe9ba62f324ceb5df0b"),
"empno" : 4,
"name" : "Sushma",
"salary" : 3000
}
{
"_id" : ObjectId("5ff7cfe9ba62f324ceb5df0c"),
"empno" : 3,
"name" : "Durga",
"salary" : 6000
}
eg-2:
-----
learners.csv:
-------------
_id name marks
1 narayan pradhan 10
2 abhilash 20
3 rasika 30
4 pankaj bhandari 40
5 Sheshanand Singh 50
6 dhanaraju 60
7 Satyasundar Panigrahi 70
8 jyothi 80
97
Q. How to insert documents from csv file
Command 1:
mongoimport --db csvdb --collection sample_csv --file
"Sample_csv_file.csv" --type csv --headerline
Command 2:
mongoimport --db csvdb --collection sample_csv --file
"Sample_csv_file.csv" --type csv --headerline --maintainInsertionOrder
Command 3:
mongoimport --db csvdb --collection sample_csv --file
"Sample_csv_file.csv" --type csv --headerline --maintainInsertionOrder --
ignoreBlanks
# --ignoreBlanks ---> Whereever the value for key is blank inside csv
file, that particular key itself will not be imported to database for that
particular document
98
1. Storage problems
2. Network traffic problem
3. Performance problems
etc
> db.learners.find().pretty()
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
{ "_id" : 3, "name" : "rasika", "marks" : 30 }
{ "_id" : 1, "name" : "narayan pradhan", "marks" : 10 }
{ "_id" : 6, "name" : "dhanaraju", "marks" : 60 }
{ "_id" : 7, "name" : "Satyasundar Panigrahi", "marks" : 70 }
{ "_id" : 5, "name" : "Sheshanand Singh", "marks" : 50 }
{ "_id" : 8, "name" : "jyothi", "marks" : 80 }
{ "_id" : 10, "name" : "bindhiya", "marks" : 100 }
{ "_id" : 9, "name" : "Hari", "marks" : 90 }
{ "_id" : 4, "name" : "pankaj bhandari", "marks" : 40 }
{ "_id" : 11, "name" : "vikas kale", "marks" : 10 }
{ "_id" : 13, "name" : "shashank sanap", "marks" : 30 }
{ "_id" : 12, "name" : "Sunita Kumati Choudhuri", "marks" : 20 }
{ "_id" : 15, "name" : "TharunK", "marks" : 50 }
99
{ "_id" : 14, "name" : "Atul", "marks" : 40 }
{ "_id" : 18, "name" : "Dusmant Kumar Mohapatra", "marks" : 80 }
{ "_id" : 16, "name" : "aron", "marks" : 60 }
{ "_id" : 24, "name" : "G.shukeshreddy", "marks" : 40 }
{ "_id" : 25, "name" : "Dakshesh", "marks" : 50 }
{ "_id" : 26, "name" : "Paramesh", "marks" : 60 }
Type "it" for more
> it
{ "_id" : 27, "name" : "Mahmodul Hasan", "marks" : 70 }
{ "_id" : 28, "name" : "ASHA", "marks" : 80 }
{ "_id" : 17, "name" : "pooja", "marks" : 70 }
{ "_id" : 30, "name" : "Maheshbabu", "marks" : 100 }
{ "_id" : 21, "name" : "Suraj Prasim Patel", "marks" : 10 }
{ "_id" : 32, "name" : "kusuma", "marks" : 20 }
{ "_id" : 33, "name" : "Vaishnavi Lende", "marks" : 30 }
{ "_id" : 19, "name" : "Deepak", "marks" : 90 }
{ "_id" : 23, "name" : "Zaid", "marks" : 30 }
{ "_id" : 20, "name" : "Bhim Kumar", "marks" : 100 }
{ "_id" : 22, "name" : "Naveen", "marks" : 20 }
{ "_id" : 35, "name" : "Prakash", "marks" : 50 }
{ "_id" : 31, "name" : "rajat kumar maurya", "marks" : 10 }
{ "_id" : 34, "name" : "Vengadesan", "marks" : 40 }
{ "_id" : 29, "name" : "Bharti Kardile", "marks" : 90 }
{ "_id" : 37, "name" : "sanat", "marks" : 70 }
{ "_id" : 38, "name" : "Aneesh Fathima", "marks" : 80 }
{ "_id" : 36, "name" : "raj", "marks" : 60 }
{ "_id" : 39, "name" : "Ratemo", "marks" : 90 }
{ "_id" : 41, "name" : "jignesh", "marks" : 10 }
Type "it" for more
> DBQuery.shellBatchSize = 5;
5
> db.learners.find().pretty()
100
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
{ "_id" : 3, "name" : "rasika", "marks" : 30 }
{ "_id" : 1, "name" : "narayan pradhan", "marks" : 10 }
{ "_id" : 6, "name" : "dhanaraju", "marks" : 60 }
{ "_id" : 7, "name" : "Satyasundar Panigrahi", "marks" : 70 }
Type "it" for more
> it
{ "_id" : 5, "name" : "Sheshanand Singh", "marks" : 50 }
{ "_id" : 8, "name" : "jyothi", "marks" : 80 }
{ "_id" : 10, "name" : "bindhiya", "marks" : 100 }
{ "_id" : 9, "name" : "Hari", "marks" : 90 }
{ "_id" : 4, "name" : "pankaj bhandari", "marks" : 40 }
Type "it" for more
Advantages of cursor:
----------------------
1. We can get only required number of documents.
2. We can get either batch wise or document wise.
3. No chance of storage problems.
4. No chance of network traffic problems.
5. No chance of performance issues.
2. hasNext()
To check whether the next document is available or not.If it
avaialble then it returns true, otherwise returns false.
3. next()
101
To get next document. If there is no next document then we will get
error.
> db.learners.find().next()
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
> db.learners.find().next()
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
In this case we are using same cursor object and hence different
documents we will get.
102
eg-1:
> var mycursor = db.emp.find()
> mycursor.hasNext()
true
> mycursor.next()
{
"_id" : ObjectId("5ff7cfe9ba62f324ceb5df09"),
"empno" : 1,
"name" : "Vidya",
"salary" : 5000
}
> mycursor.next()
{
"_id" : ObjectId("5ff7cfe9ba62f324ceb5df0a"),
"empno" : 2,
"name" : "Ravi",
"salary" : 2000
}
eg-2:
var mycursor = db.learners.find();
while( mycursor.hasNext() )
{
print(tojson(mycursor.next()));
}
on shell:
var mycursor = db.learners.find();
while( mycursor.hasNext() ) { print(tojson(mycursor.next())); }
103
eg-3:
-----
var mycursor = db.learners.find();
while( mycursor.hasNext() )
{
printjson(mycursor.next());
}
on shell:
var mycursor = db.learners.find();
while( mycursor.hasNext() ) { printjson(mycursor.next()); }
eg-4:
-----
var mycursor = db.learners.find();
mycursor.forEach( doc => { printjson(doc) } )
eg-5:
-----
var mycursor = db.learners.find();
mycursor.forEach(printjson)
1. limit()
2. skip()
3. sort()
1. limit():
104
-----------
We can use this limit() method to limit the number of documents in the
result.
> db.learners.find().count()
6571
> db.learners.find().limit(1)
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
> db.learners.find().limit(2)
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
{ "_id" : 3, "name" : "rasika", "marks" : 30 }
> db.learners.find().limit(5)
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
{ "_id" : 3, "name" : "rasika", "marks" : 30 }
{ "_id" : 1, "name" : "narayan pradhan", "marks" : 10 }
{ "_id" : 6, "name" : "dhanaraju", "marks" : 60 }
{ "_id" : 7, "name" : "Satyasundar Panigrahi", "marks" : 70 }
> db.learners.find().limit(25)
{ "_id" : 2, "name" : "abhilash", "marks" : 20 }
{ "_id" : 3, "name" : "rasika", "marks" : 30 }
{ "_id" : 1, "name" : "narayan pradhan", "marks" : 10 }
{ "_id" : 6, "name" : "dhanaraju", "marks" : 60 }
{ "_id" : 7, "name" : "Satyasundar Panigrahi", "marks" : 70 }
{ "_id" : 5, "name" : "Sheshanand Singh", "marks" : 50 }
{ "_id" : 8, "name" : "jyothi", "marks" : 80 }
{ "_id" : 10, "name" : "bindhiya", "marks" : 100 }
{ "_id" : 9, "name" : "Hari", "marks" : 90 }
{ "_id" : 4, "name" : "pankaj bhandari", "marks" : 40 }
105
{ "_id" : 11, "name" : "vikas kale", "marks" : 10 }
{ "_id" : 13, "name" : "shashank sanap", "marks" : 30 }
{ "_id" : 12, "name" : "Sunita Kumati Choudhuri", "marks" : 20 }
{ "_id" : 15, "name" : "TharunK", "marks" : 50 }
{ "_id" : 14, "name" : "Atul", "marks" : 40 }
{ "_id" : 18, "name" : "Dusmant Kumar Mohapatra", "marks" : 80 }
{ "_id" : 16, "name" : "aron", "marks" : 60 }
{ "_id" : 24, "name" : "G.shukeshreddy", "marks" : 40 }
{ "_id" : 25, "name" : "Dakshesh", "marks" : 50 }
{ "_id" : 26, "name" : "Paramesh", "marks" : 60 }
Type "it" for more
> it
{ "_id" : 27, "name" : "Mahmodul Hasan", "marks" : 70 }
{ "_id" : 28, "name" : "ASHA", "marks" : 80 }
{ "_id" : 17, "name" : "pooja", "marks" : 70 }
{ "_id" : 30, "name" : "Maheshbabu", "marks" : 100 }
{ "_id" : 21, "name" : "Suraj Prasim Patel", "marks" : 10 }
2. skip():
---------
We can use skip() method to skip the number of documents in the
result.
> db.learners.find().skip(10)
To skip the first 10 documents.
Use Case:
---------
In general we can use skip() and limit() methods in pagination concept
while displaying our data.
106
Assume per page 10 documents:
To display 1st page: db.learners.find().limit(10)
To display 2nd page: db.learners.find().skip(10).limit(10)
To display 3rd page: db.learners.find().skip(20).limit(10)
etc
3. sort():
----------
We can use sort() method to sort documents based on value of a
particular field.
Syntax:
sort({ field: 1})
1 ===> means Ascending order/Alphabetical order
-1 ===> means Descending order/ Reverse of Alphabetical order
107
Sorting is based on field1, if field1 values are same then sorting based
on field2 for those documents.
Note:
-----
Chaining of these helper methods is possible.
All these methods will be executed from left to right and hence order
is important.
108
We can get documents with only required fields instead of all fields.
This is called projection.
Projection in MongoDB?
----------------------
db.collection.find({filter}) ===>without projection
db.collection.find({filter},{projection fields}) ===>with projection
Case Study:
----------
books collection: sample document
{
"title": "Linux in simple way",
"isbn": 6677,
"downloadable": false,
"no_of_reviews": 1,
"tags": ["os","freeware","shell programming"],
"languages": ["english","hindi","telugu"],
"author": {
"name": "Shiva Ramachandran",
109
"callname": "Shiv",
"profile": {
"exp":8,
"courses":3,
"books":2
}
}
}
db.collection.find({},{projection fields})
If we are not taking any field in the projected list, bydefault that field
will be excluded. ie default value is 0.
_id field will be included always. But we can exclude this field by
assigning with 0 explicitly.
110
"no_of_reviews" : 1
}
{
"_id" : ObjectId("5fe95428fe935cdac43627cb"),
"title" : "MongoDB in simple way",
"no_of_reviews" : 4
}
{
"_id" : ObjectId("5fe95428fe935cdac43627cc"),
"title" : "Python in simple way",
"no_of_reviews" : 5
}
{
"_id" : ObjectId("5fe95428fe935cdac43627cd"),
"title" : "Shell Scripting in simple way",
"no_of_reviews" : 1
}
{
"_id" : ObjectId("5fe95428fe935cdac43627ce"),
"title" : "Devops in simple way",
"no_of_reviews" : 3
}
{
"_id" : ObjectId("5fe95428fe935cdac43627cf"),
"title" : "Oracle in simple way",
"no_of_reviews" : 3
}
Note:
> db.books.find({},{}).pretty()
We will get all documents with all fields. Simply it is equals to:
> db.books.find().pretty()
111
Q2. To project only title and no_of_reviews without _id ?
> db.books.find({},{title: 1,no_of_reviews: 1, _id: 0}).pretty()
112
> db.books.find({},{title: 1, "author.name": 1, "author.profile.books":1,
_id:0 }).pretty()
{
"title" : "Java in simple way",
"author" : {
"name" : "Karhik Ramachandran",
"profile" : {
"books" : 3
}
}
}
Projection of arrays:
--------------------
Q. Project title, tags in every document of books collection?
> db.books.find({},{ title:1, tags: 1, _id:0}).pretty()
{
"title" : "Java in simple way",
"tags" : [
"language",
"freeware",
"programming"
]
}
{
"title" : "Linux in simple way",
"tags" : [
"os",
"freeware",
"shell programming"
]
113
}
1. $ Operator:
--------------
We can use $ operator to project first element in an array that
matches query condition.
Syntax:
db.collection.find({<array>:<condition>,...},{"<array>.$":1})
Case Study:
-----------
db.students.insertOne({_id:1, name:"Durga", year:1, marks:[70,87,90]})
db.students.insertOne({_id:2, name:"Ravi", year:1, marks:[90,88,92]})
db.students.insertOne({_id:3, name:"Shiva", year:1, marks:[85,100,90]})
db.students.insertOne({_id:4, name:"Durga", year:2, marks:[79,85,80]})
db.students.insertOne({_id:5, name:"Ravi", year:2, marks:[88,88,92]})
db.students.insertOne({_id:6, name:"Shiva", year:2, marks:[95,90,96]})
114
Q1. db.students.find({marks:{$gte: 85}},{_id:0,marks:1})
eg-1:
> db.students.find({},{_id:0,name: 1, "marks.$":1})
Error: error: {
115
"ok" : 0,
"errmsg" : "positional operator '.$' couldn't find a matching
element in the array",
"code" : 51246,
"codeName" : "Location51246"
}
eg1:
> db.students.find({year: 1},{_id:0,name: 1, "marks.$":1})
Error: error: {
"ok" : 0,
"errmsg" : "positional operator '.$' couldn't find a matching
element in the array",
"code" : 51246,
"codeName" : "Location51246"
}
2. $elemMatch operator:
-----------------------
1. selects only one element
2. which is matched element where condition is specified by
$elemMatch explicitly.
It never considers query condition.
Q1.
116
> db.students.find({},{_id:0, name:1,year:1,marks:{$elemMatch:{$lt:
95}}})
> db.students.find({year:1},{_id:0,
name:1,year:1,marks:{$elemMatch:{$gt: 85}}})
{ "name" : "Durga", "year" : 1, "marks" : [ 87 ] }
{ "name" : "Ravi", "year" : 1, "marks" : [ 90 ] }
{ "name" : "Shiva", "year" : 1, "marks" : [ 100 ] }
117
> db.students.find({year:1,marks:{$gte: 85}},{_id:0,name:1,"marks.$":1})
{ "name" : "Durga", "marks" : [ 87 ] }
{ "name" : "Ravi", "marks" : [ 90 ] }
{ "name" : "Shiva", "marks" : [ 85 ] }
> db.students.find({year:1,marks:{$gte:
85}},{_id:0,name:1,marks:{$elemMatch:{$gt:89}}})
3. $slice operator:
-------------------
By using $slice operator we can select required number of elements in
the array.
Syntax-1:
---------
db.collection.find({query},{<array>:{$slice: n}})
eg-1:
> db.students.find({},{_id:0,name:1,year:1, marks:{$slice: 2}})
In the array only first 2 elements will be selected.
118
{ "name" : "Durga", "year" : 1, "marks" : [ 70, 87 ] }
{ "name" : "Ravi", "year" : 1, "marks" : [ 90, 88 ] }
{ "name" : "Shiva", "year" : 1, "marks" : [ 85, 100 ] }
{ "name" : "Durga", "year" : 2, "marks" : [ 79, 85 ] }
{ "name" : "Ravi", "year" : 2, "marks" : [ 88, 88 ] }
{ "name" : "Shiva", "year" : 2, "marks" : [ 95, 90 ] }
eg-2:
> db.students.find({},{_id:0,name:1,year:1, marks:{$slice: -2}})
In the array only last 2 elements will be selected.
eg-3:
> db.students.find({},{_id:0,name:1,year:1, marks:{$slice: 100}})
In this case all elements will be included.
Syntax-2:
---------
119
db.collection.find({query},{<array>:{$slice: [n1,n2]}})
skip n1 number of elements and then select n2 number of elements.
n1--->number to skip
n2--->number to return
eg-1:
skip first element and then select next two elements.
CRUD Operations
C--->Create Operation | Insert Operation
R--->Retrieve Operation | Read Operation
U--->Update Operation
120
U--->Update Operation:
----------------------
students collection
update methods:
---------------
There are 3 update methods are avaialble.
1. updateOne()
2. updateMany()
3. update()
121
1. updateOne():
---------------
db.collection.updateOne(filter,update,options)
It finds the first document that matches filter criteria and perform
required updation. It will perform updation for a single document.
2. updateMany():
---------------
db.collection.updateMany(filter,update,options)
To update all documents that match the specified filter criteria.
3. update():
-----------
db.collection.update(filter,update,options)
Case Study:
-----------
db.employees.insert({_id:1,eno:100,ename:"Sunny",esal:1000,eaddr:"M
umbai"})
122
db.employees.insert({_id:2,eno:200,ename:"Bunny",esal:2000,eaddr:"H
yderabad"})
db.employees.insert({_id:3,eno:300,ename:"Chinny",esal:3000,eaddr:"M
umbai"})
db.employees.insert({_id:4,eno:400,ename:"Vinny",esal:4000,eaddr:"De
lhi"})
db.employees.insert({_id:5,eno:500,ename:"Pinny",esal:5000,eaddr:"Ch
ennai"})
db.employees.insert({_id:6,eno:600,ename:"Tinny",esal:6000,eaddr:"Mu
mbai"})
db.employees.insert({_id:7,eno:700,ename:"Zinny",esal:7000,eaddr:"De
lhi"})
123
Q2. Update all Mumbai based employees salary as 7777?
124
It will update all matched documents
125
> db.employees.find({eaddr: "Delhi"})
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 5555, "eaddr" :
"Delhi" }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 7000, "eaddr" :
"Delhi" }
Update Operators:
----------------
We can use update operators to perform required updations.
1. $set
2. $unset
3. $rename
4. $inc
5. $min
6. $max
7. $mul
etc
126
1. $set operator:
-----------------
We can use $set operator to set the value to the field in matched
document.
case-1:
-------
If the specified field does not exist, $set will add a new field with
provided value.
> db.employees.find({ename:"Sunny"})
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 7777, "eaddr" :
"Mumbai" }
> db.employees.update({ename:"Sunny"},{$set: {husband: "Daniel"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.employees.find({ename:"Sunny"})
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 7777, "eaddr" :
"Mumbai", "husband" : "Daniel" }
127
> db.employees.find({ename:"Sunny"})
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 7777, "eaddr" :
"Mumbai", "husband" : "Daniel" }
> db.employees.update({ename:"Sunny"},{$set: {esal: 1111, age: 45,
origin: "Punjab"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.employees.find({ename:"Sunny"})
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1111, "eaddr" :
"Mumbai", "husband" : "Daniel", "age" : 45, "origin" : "Punjab" }
Q3. Add a new field named with friend with value Guest where esal
value is >= 4000?
128
> db.employees.find({esal:{$gte: 4000}})
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 7777, "eaddr" :
"Mumbai", "friend" : "Guest" }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4444, "eaddr" :
"Delhi", "friend" : "Guest" }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5000, "eaddr" :
"Chennai", "friend" : "Guest" }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 7777, "eaddr" :
"Mumbai", "friend" : "Guest" }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 4444, "eaddr" :
"Delhi", "friend" : "Guest" }
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1111, "eaddr" :
"Mumbai", "husband" : "Daniel", "age" : 45, "origin" : "Punjab" }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 2000, "eaddr" :
"Hyderabad" }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 7777, "eaddr" :
"Mumbai", "friend" : "Guest" }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4444, "eaddr" :
"Delhi", "friend" : "Guest" }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5000, "eaddr" :
"Chennai", "friend" : "Guest" }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 7777, "eaddr" :
"Mumbai", "friend" : "Guest" }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 4444, "eaddr" :
"Delhi", "friend" : "Guest" }
129
"downloadable" : true,
"no_of_reviews" : 3,
"tags" : [
"database",
"sql",
"relational"
],
"languages" : [
"english",
"hindi",
"telugu"
],
"author" : {
"name" : "Virat Kohli",
"callname" : "kohli",
"profile" : {
"exp" : 2,
"courses" : 2,
"books" : 2
}
}
}
130
"downloadable" : true,
"no_of_reviews" : 3,
"tags" : [
"database",
"sql",
"relational"
],
"languages" : [
"english",
"hindi",
"telugu"
],
"author" : {
"name" : "Virat Kohli",
"callname" : "kohli",
"profile" : {
"exp" : 2,
"courses" : 2,
"books" : 2
}
}
}
> db.books.update({"author.name": "Virat Kohli"},{$set:
{"author.callname": "Virushka"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.books.find({"author.name": "Virat Kohli"}).pretty()
{
"_id" : ObjectId("5fe95428fe935cdac43627cf"),
"title" : "Oracle in simple way",
"isbn" : 6677,
"downloadable" : true,
"no_of_reviews" : 3,
"tags" : [
131
"database",
"sql",
"relational"
],
"languages" : [
"english",
"hindi",
"telugu"
],
"author" : {
"name" : "Virat Kohli",
"callname" : "Virushka",
"profile" : {
"exp" : 2,
"courses" : 2,
"books" : 2
}
}
}
2. $unset operator:
-------------------
To delete the specified field.
Syntax:
{$unset: {field1:"",field2:"",...}}
The specified value in the $unset expression (ie "") does not impact
operation.
132
> db.employees.find({ename: "Sunny"})
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1000, "eaddr" :
"Mumbai", "husband" : "Daniel" }
> db.employees.update({ename: "Sunny"},{$unset: {esal:0,husband:""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.employees.find({ename: "Sunny"})
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "eaddr" : "Mumbai" }
Note: If the specified field is not avaialble, then $unset operator won't
do anything.
Q. Remove fields husband and friend where esal is less than 8000?
> db.employees.updateMany({esal:{$lt:8000}}, {$unset:
{husband:"",friend:""}})
3. $rename operator:
--------------------
We can use $rename operator to rename fields, ie to change name of
the field.
Syntax:
{$rename: {field1:<newName1>, field2:<newName2>, ...} }
133
Q. Write Query to rename esal as salary and eaddr as city in
employees collection?
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1000, "eaddr" :
"Mumbai" }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 2000, "eaddr" :
"Hyderabad" }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 3000, "eaddr" :
"Mumbai" }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4000, "eaddr" :
"Delhi" }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5000, "eaddr" :
"Chennai" }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 6000, "eaddr" :
"Mumbai" }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 7000, "eaddr" :
"Delhi" }
> db.employees.updateMany({},{$rename: {esal:"salary", eaddr: "city"}})
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "city" : "Mumbai", "salary" :
1000 }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "city" : "Hyderabad", "salary"
: 2000 }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "city" : "Mumbai", "salary" :
3000 }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "city" : "Delhi", "salary" :
4000 }
134
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "city" : "Chennai", "salary" :
5000 }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "city" : "Mumbai", "salary" :
6000 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "city" : "Delhi", "salary" :
7000 }
Note:
-----
1. The $rename operator internally performs $unset of both old name
and new name and then performs $set with new name. Hence it won't
preserve order of fields.
eg:
>
db.employees.insert({_id:8,eno:800,esal:8000,eaddr:"Hyderabad",city:"
Mumbai"})
> db.employees.find()
> db.employees.find({_id:8})
{ "_id" : 8, "eno" : 800, "esal" : 8000, "eaddr" : "Hyderabad", "city" :
"Mumbai" }
> db.employees.update({_id:8},{$rename: {eaddr:"city"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.employees.find({_id:8})
135
{ "_id" : 8, "eno" : 800, "esal" : 8000, "city" : "Hyderabad" }
3. If the field to rename does not exist in the document then $rename
won't do anything.
> db.employees.find({_id:8})
{ "_id" : 8, "eno" : 800, "esal" : 8000, "city" : "Hyderabad" }
> db.employees.update({_id:8},{$rename: {age:"totalage"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.employees.find({_id:8})
{ "_id" : 8, "eno" : 800, "esal" : 8000, "city" : "Hyderabad" }
4. $inc operator:
-----------------
inc means increment.
We can use $inc to increment or decrement value of the field with
specified amount.
salary=salary+1000
salary=salary-1000
Syntax:
{$inc: {field1:amount1,field2:amount,..}}
case study:
----------
136
db.employees.insert({_id:1,eno:100,ename:"Sunny",esal:1000,eaddr:"M
umbai"})
db.employees.insert({_id:2,eno:200,ename:"Bunny",esal:2000,eaddr:"H
yderabad"})
db.employees.insert({_id:3,eno:300,ename:"Chinny",esal:3000,eaddr:"M
umbai"})
db.employees.insert({_id:4,eno:400,ename:"Vinny",esal:4000,eaddr:"De
lhi"})
db.employees.insert({_id:5,eno:500,ename:"Pinny",esal:5000,eaddr:"Ch
ennai"})
db.employees.insert({_id:6,eno:600,ename:"Tinny",esal:6000,eaddr:"Mu
mbai"})
db.employees.insert({_id:7,eno:700,ename:"Zinny",esal:7000,eaddr:"De
lhi"})
137
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 6500, "eaddr" :
"Mumbai" }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 7500, "eaddr" :
"Delhi" }
Note:
1. If the specified field does not exist, $inc creates that field and sets
that field to the specified value.
138
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1500, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 2500, "eaddr" :
"Hyderabad", "age" : 2 }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 3500, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4500, "eaddr" :
"Delhi", "age" : 2 }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5499, "eaddr" :
"Chennai", "age" : 2 }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 6499, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 7499, "eaddr" :
"Delhi", "age" : 2 }
$set--->To set a new value to the specified field/To create a new field
with provided value
$unset--->To delete specified field
$rename--->To rename the specified field
$inc--->To perform increment and decrement operations on the field
value
139
----------------------------
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1500, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 2500, "eaddr" :
"Hyderabad", "age" : 2 }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 3500, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4500, "eaddr" :
"Delhi", "age" : 2 }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5499, "eaddr" :
"Chennai", "age" : 2 }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 6499, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 7499, "eaddr" :
"Delhi", "age" : 2 }
4. $min operator:
-----------------
It only updates field value if the specified value is less than current
field value .
minimum value of(provided value,current value)
Consider only the value which is minimum among given and current
140
Syntax: {$min: {field1:value1, field2:value2,...}}
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 1500, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 2500, "eaddr" :
"Hyderabad", "age" : 2 }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 3500, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4500, "eaddr" :
"Delhi", "age" : 2 }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5000, "eaddr" :
"Chennai", "age" : 2 }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 5000, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 5000, "eaddr" :
"Delhi", "age" : 2 }
Note: If the specified field does not exist, then $min operator creates
that field and assign with provided value.
> db.employees.updateMany({},{$min:{marks: 99}})
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
6. $max operator:
-----------------
141
$max operator updates the value of the field to the specified value iff
specified value is greater than current value.
142
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 5000, "eaddr" :
"Mumbai", "age" : 2 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 5000, "eaddr" :
"Delhi", "age" : 2 }
Note: If the specified field does not exist, then $max operator creates
that field and assign with provided value.
> db.employees.updateMany({},{$max:{height:5.8}})
> db.employees.updateMany({},{$max:{height:5.8}})
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 4000, "eaddr" :
"Mumbai", "age" : 2, "marks" : 99, "height" : 5.8 }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 4000, "eaddr" :
"Hyderabad", "age" : 2, "marks" : 99, "height" : 5.8 }
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 4000, "eaddr" :
"Mumbai", "age" : 2, "marks" : 99, "height" : 5.8 }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 4500, "eaddr" :
"Delhi", "age" : 2, "marks" : 99, "height" : 5.8 }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5000, "eaddr" :
"Chennai", "age" : 2, "marks" : 99, "height" : 5.8 }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 5000, "eaddr" :
"Mumbai", "age" : 2, "marks" : 99, "height" : 5.8 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 5000, "eaddr" :
"Delhi", "age" : 2, "marks" : 99, "height" : 5.8 }
Note:
1. If provided value is less than current value then only perform
updation-->min operator.
2. If provided value is greater than current value then only perform
updation-->max operator.
143
Q. what if we don't want min/max operator to create the new field?
Select all documents where specified field exists and then perform
updation.
> db.employees.update({age:{$exists:true}},{$set:{age:15}},{multi:true})
WriteResult({ "nMatched" : 7, "nUpserted" : 0, "nModified" : 7 })
>
db.employees.update({phone_number:{$exists:true}},{$set:{phone_num
ber:1234}},{multi:true})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
7. $mul operator:
-----------------
mul means multiplication
Q1. Double all employee salary where esal is less than 4900?
> db.employees.updateMany({esal: {$lt: 4900}},{$mul: {esal: 2}})
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 }
1000--->1100
144
10000--->11000
Note:
If the specified field is not available then $mul creates that field and
sets the value to zero.
Note:
1. $set --->To set a new value to the existing field or to create new
field
2. $unset--->To unset/delete existing field
3. $rename-->To rename the value of the field.
4. $inc --->To increment or decrement field value
5. $min --->To update only if the provided value is less than current
value
6. $max --->To update only if the provided value is greater than current
value
7. $mul -->To multiply field value by a number
If the document not available then we can insert that document in the
database automatically. For this we have to use upsert property.
145
upsert = update + insert
at the time two works update and insert. First update and if it is not
possible then insert.
Demo Execution:
---------------
>db.employees.find()
> db.employees.find()
{ "_id" : 1, "eno" : 100, "ename" : "Sunny", "esal" : 8800, "eaddr" :
"Mumbai", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0 }
{ "_id" : 2, "eno" : 200, "ename" : "Bunny", "esal" : 8000, "eaddr" :
"Hyderabad", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0
}
{ "_id" : 3, "eno" : 300, "ename" : "Chinny", "esal" : 8800, "eaddr" :
"Mumbai", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0 }
{ "_id" : 4, "eno" : 400, "ename" : "Vinny", "esal" : 9000, "eaddr" :
"Delhi", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0 }
{ "_id" : 5, "eno" : 500, "ename" : "Pinny", "esal" : 5000, "eaddr" :
"Chennai", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0 }
{ "_id" : 6, "eno" : 600, "ename" : "Tinny", "esal" : 5500, "eaddr" :
"Mumbai", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0 }
{ "_id" : 7, "eno" : 700, "ename" : "Zinny", "esal" : 5000, "eaddr" :
"Delhi", "age" : 15, "marks" : 99, "height" : 5.8, "abc" : 0, "xyz" : 0 }
{ "_id" : 10, "ename" : "Durga", "eaddr" : "Hyderabad", "eno" : 1000,
"esal" : 10000, "abc" : 0, "xyz" : 0 }
146
> db.employees.update({ename: "Mallika"}, {$set:
{_id:11,esal:9999,eaddr:"Mumabi"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.employees.find()
...
{ "_id" : 11, "ename" : "Mallika", "eaddr" : "Mumabi", "esal" : 9999 }
Syntax:
db.collection.update(query,{update_operator:{"<array>.$" : value}})
147
Case Study:
-----------
db.students.insertOne({_id:1, marks: [70,87,90,30,40]})
db.students.insertOne({_id:2, marks: [90,88,92,110,45]})
db.students.insertOne({_id:3, marks: [85,100,90,76,58]})
db.students.insertOne({_id:4, marks: [79,85,80,89,56]})
db.students.insertOne({_id:5, marks: [88,88,92,45,23]})
db.students.insertOne({_id:6, marks: [95,90,96,92,95]})
Q1. Update the first matched element 90 in marks array to 999 where
_id:1?
> db.students.update({_id:1,marks:90},{$set: {"marks.$": 999}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : 1, "marks" : [ 70, 87, 90, 30, 40 ] }
{ "_id" : 2, "marks" : [ 90, 88, 92, 110, 45 ] }
{ "_id" : 3, "marks" : [ 85, 100, 90, 76, 58 ] }
{ "_id" : 4, "marks" : [ 79, 85, 80, 89, 56 ] }
{ "_id" : 5, "marks" : [ 88, 88, 92, 45, 23 ] }
{ "_id" : 6, "marks" : [ 95, 90, 96, 92, 95 ] }
> db.students.update({_id:1,marks:90},{$set: {"marks.$": 999}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : 1, "marks" : [ 70, 87, 999, 30, 40 ] }
{ "_id" : 2, "marks" : [ 90, 88, 92, 110, 45 ] }
{ "_id" : 3, "marks" : [ 85, 100, 90, 76, 58 ] }
{ "_id" : 4, "marks" : [ 79, 85, 80, 89, 56 ] }
{ "_id" : 5, "marks" : [ 88, 88, 92, 45, 23 ] }
{ "_id" : 6, "marks" : [ 95, 90, 96, 92, 95 ] }
148
Q2. Update the first matched element in marks array which is less
than 90 with 90 in every document?
> db.students.updateMany({marks:{$elemMatch:{$lt:90}}},{$set:
{"marks.$": 90}})
> db.students.find()
{ "_id" : 1, "marks" : [ 70, 87, 999, 30, 40 ] }
{ "_id" : 2, "marks" : [ 90, 88, 92, 110, 45 ] }
{ "_id" : 3, "marks" : [ 85, 100, 90, 76, 58 ] }
{ "_id" : 4, "marks" : [ 79, 85, 80, 89, 56 ] }
{ "_id" : 5, "marks" : [ 88, 88, 92, 45, 23 ] }
{ "_id" : 6, "marks" : [ 95, 90, 96, 92, 95 ] }
> db.students.updateMany({marks:{$elemMatch:{$lt:90}}},{$set:
{"marks.$": 90}})
{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
> db.students.find()
{ "_id" : 1, "marks" : [ 90, 87, 999, 30, 40 ] }
{ "_id" : 2, "marks" : [ 90, 90, 92, 110, 45 ] }
{ "_id" : 3, "marks" : [ 90, 100, 90, 76, 58 ] }
{ "_id" : 4, "marks" : [ 90, 85, 80, 89, 56 ] }
{ "_id" : 5, "marks" : [ 90, 88, 92, 45, 23 ] }
{ "_id" : 6, "marks" : [ 95, 90, 96, 92, 95 ] }
Syntax:
db.collection.update(query,{update_operator:{"<array>.$[]" : value}})
149
Q1. To increment every element of marks array by 10?
> db.students.updateMany({marks:{$elemMatch:{$gte:1000}}},{$set:
{"marks.$[]": 1000}})
> db.students.find()
{ "_id" : 1, "marks" : [ 100, 97, 1009, 40, 50 ] }
{ "_id" : 2, "marks" : [ 100, 100, 102, 120, 55 ] }
{ "_id" : 3, "marks" : [ 100, 110, 100, 86, 68 ] }
150
{ "_id" : 4, "marks" : [ 100, 95, 90, 99, 66 ] }
{ "_id" : 5, "marks" : [ 100, 98, 102, 55, 33 ] }
{ "_id" : 6, "marks" : [ 105, 100, 106, 102, 105 ] }
> db.students.updateMany({marks:{$elemMatch:{$gte:1000}}},{$set:
{"marks.$[]": 1000}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.students.find()
{ "_id" : 1, "marks" : [ 1000, 1000, 1000, 1000, 1000 ] }
{ "_id" : 2, "marks" : [ 100, 100, 102, 120, 55 ] }
{ "_id" : 3, "marks" : [ 100, 110, 100, 86, 68 ] }
{ "_id" : 4, "marks" : [ 100, 95, 90, 99, 66 ] }
{ "_id" : 5, "marks" : [ 100, 98, 102, 55, 33 ] }
{ "_id" : 6, "marks" : [ 105, 100, 106, 102, 105 ] }
Syntax:
Q. Update all marks array elements which are less than 100 as 100?
151
> db.students.updateMany({},{$set: {"marks.$[element]":
100}},{arrayFilters:[{"element": {$lt: 100}}]})
> db.students.find()
{ "_id" : 1, "marks" : [ 1000, 1000, 1000, 1000, 1000 ] }
{ "_id" : 2, "marks" : [ 100, 100, 102, 120, 55 ] }
{ "_id" : 3, "marks" : [ 100, 110, 100, 86, 68 ] }
{ "_id" : 4, "marks" : [ 100, 95, 90, 99, 66 ] }
{ "_id" : 5, "marks" : [ 100, 98, 102, 55, 33 ] }
{ "_id" : 6, "marks" : [ 105, 100, 106, 102, 105 ] }
> db.students.updateMany({},{$set: {"marks.$[element]":
100}},{arrayFilters:[{"element": {$lt: 100}}]})
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 4 }
> db.students.find()
{ "_id" : 1, "marks" : [ 1000, 1000, 1000, 1000, 1000 ] }
{ "_id" : 2, "marks" : [ 100, 100, 102, 120, 100 ] }
{ "_id" : 3, "marks" : [ 100, 110, 100, 100, 100 ] }
{ "_id" : 4, "marks" : [ 100, 100, 100, 100, 100 ] }
{ "_id" : 5, "marks" : [ 100, 100, 102, 100, 100 ] }
{ "_id" : 6, "marks" : [ 105, 100, 106, 102, 105 ] }
152
> db.students.updateMany({},{$set: {"marks.$[e1]": 110}},{arrayFilters:
[{$and: [{"e1":{$gt:100}},{"e1":{$lte:110}}]}]})
> db.students.find()
{ "_id" : 1, "marks" : [ 1000, 1000, 1000, 1000, 1000 ] }
{ "_id" : 2, "marks" : [ 100, 100, 102, 120, 100 ] }
{ "_id" : 3, "marks" : [ 100, 110, 100, 100, 100 ] }
{ "_id" : 4, "marks" : [ 100, 100, 100, 100, 100 ] }
{ "_id" : 5, "marks" : [ 100, 100, 102, 100, 100 ] }
{ "_id" : 6, "marks" : [ 105, 100, 106, 102, 105 ] }
> db.students.updateMany({},{$set: {"marks.$[e1]": 110}},{arrayFilters:
[{$and: [{"e1":{$gt:100}},{"e1":{$lte:110}}]}]})
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 3 }
> db.students.find()
{ "_id" : 1, "marks" : [ 1000, 1000, 1000, 1000, 1000 ] }
{ "_id" : 2, "marks" : [ 100, 100, 110, 120, 100 ] }
{ "_id" : 3, "marks" : [ 100, 110, 100, 100, 100 ] }
{ "_id" : 4, "marks" : [ 100, 100, 100, 100, 100 ] }
{ "_id" : 5, "marks" : [ 100, 100, 110, 100, 100 ] }
{ "_id" : 6, "marks" : [ 110, 100, 110, 110, 110 ] }
153
If the marks in the range 91 to 100 make as 100
db.students.updateMany(
{},
{
$set: {
"marks.$[e1]": 80,
"marks.$[e2]": 90,
"marks.$[e3]": 100
}
},
{
arrayFilters: [
{$and: [{"e1":{$gt:70}},{"e1":{$lte:80}}]},
{$and: [{"e2":{$gt:80}},{"e2":{$lte:90}}]},
{$and: [{"e3":{$gt:90}},{"e3":{$lte:100}}]}
]
}
)
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.students.find()
{ "_id" : 1, "marks" : [ 70, 90, 90 ] }
{ "_id" : 2, "marks" : [ 90, 90, 100 ] }
{ "_id" : 3, "marks" : [ 90, 100, 90 ] }
{ "_id" : 4, "marks" : [ 80, 90, 80 ] }
{ "_id" : 5, "marks" : [ 90, 90, 100 ] }
154
{ "_id" : 6, "marks" : [ 100, 90, 100 ] }
Note:
$ --->To update only first matched element of array
$[] --->To update all elements of array
$[identifier] ---> To update specific array elements
Syntax:
db.collection.update({},{$push: {<array1>: value1,...}})
155
It is not added 3 elements only one element added.
Reason: In Javascript object, duplicate keys are not allowed. If we are
trying to add entry with duplicate key, old value will be replaced with
new value.
{marks: 36,marks: 37, marks: 38} ===>{marks: 38}
2nd way:
------
> db.students.update({_id:1}, {$push: {marks: [39,40,41]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : 1, "marks" : [ 70, 90, 90, 35, 38, [ 39, 40, 41 ] ] }
$each modifier:
---------------
We can use $each modifier to add multiple values to the array.
Syntax:
{ $push: { <array>: {$each: [value1,value2,...]} }
$position modifier:
--------------------
156
Bydefault elements will be added at the end of the array. But we can
add elements in the required position. For this we have to use
$position modifier.
Syntax:
{
$push: {
<array>: {
$each: [value1,value2,value3],
$position: <num>
}
}
}
> db.students.update(
{_id:7},
{
$push: {
marks: {
$each:[50],
$position:0
}
157
}
}
)
Negative Index:
---------------
We can use negative index to add elements from the end. -1 indicates
the position just before last element in the array.
eg-1:
> db.students.update(
{_id:7},
{
$push: {
marks: {
158
$each:[70],
$position:-1
}
}
}
)
{ "_id" : 7, "marks" : [ 50, 10, 20, 60, 30, 70, 40 ] }
eg-2:
db.students.update(
{_id:7},
{
$push: {
marks: {
$each:[80],
$position:-2
}
}
}
)
{ "_id" : 7, "marks" : [ 50, 10, 20, 60, 30, 80, 70, 40 ] }
eg-3:
db.students.update(
{_id:7},
{
$push: {
marks: {
$each:[1,2,3],
159
$position:-3
}
}
}
)
{ "_id" : 7, "marks" : [ 50, 10, 20, 60, 30, 1, 2, 3, 80, 70, 40 ] }
$sort modifier:
--------------
We can use $sort modifier to sort elements of the array while
performing push operation.
We can pass empty array [], to $each modifier to see effect of only
$sort.
Syntax:
-------
db.collection.update(
{},
{
$push: {
<array>: { $each: [value1,value2,..],
$sort: 1|-1
}
}
160
})
{ "_id" : 7, "marks" : [ 1, 2, 3, 10, 10, 15, 20, 25, 30, 34, 35, 36, 40, 60,
70, 80, 98, 99, 100, 897 ] }
$slice modifier:
---------------
The $slice modifier limits the number of array elements during $push
operation.
161
We can pass empty array [], to $each modifier to see effect of only
$slice modifier.
Syntax:
------
db.collection.update(
{},
{
$push: {
<array>: { $each: [value1,value2,..],
$slice: <num>
}
}
})
{ "_id" : 7, "marks" : [ 3, 2, 1, 5, 6, 7 ] }
162
eg-2: To update array with first 3 elements:
--------------------------------------------
> db.students.update({_id: 7},{$push: {marks: {$each:[],$slice: 3}}})
{ "_id" : 7, "marks" : [ 3, 2, 1 ] }
eg:
> db.students.update(
{_id:7},
{
$push: {marks: {$slice:3, $sort: -1, $each:[4,1,7,2,6,3,9,2,8,4,5]}}
}
)
{ "_id" : 7, "marks" : [ 9, 8, 7 ] }
163
Note:
1. If the spcified array is not already available then $push adds that
array field with values as its elements.
Summary:
--------
$push operator --->To add elements to array.
$each modifier --->To add multiple elements
$position modifier --->To add elements at specified position
$sort modifier --->To sort elements after addition
$slice modifier --->To limit the number of elements.
5. $addToSet operator:
----------------------
It is exactly same as $push operator except that it won't allow
duplicates.
It adds elements to the array iff array does not contain already those
elements.
There is no effect on already existing duplicates.
164
case study:
-----------
db.students.insertOne({_id:1,marks:[70,87,90]})
db.students.insertOne({_id:2,marks:[90,88,92]})
db.students.insertOne({_id:3,marks:[85,100,90]})
db.students.insertOne({_id:4,marks:[79,85,80]})
db.students.insertOne({_id:5,marks:[88,88,92]})
db.students.insertOne({_id:6,marks:[95,90,96]})
165
{ "_id" : 5, "marks" : [ 88, 88, 92, 90, 10, 20, 30 ] }
Syntax:
------
{
$pop: {<array>:-1|1}
}
166
eg-1: To remove first element:
------------------------------
{ "_id" : 5, "marks" : [ 88, 88, 92, 90, 10, 20, 30 ] }
> db.students.update({_id: 5},{$pop: {marks: -1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{ "_id" : 5, "marks" : [ 88, 92, 90, 10, 20, 30 ] }
Syntax:
-------
{
$pull: {<array>: <value> | <condition> }
}
167
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{ "_id" : 7, "marks" : [ 20, 30, 20, 40, 50, 60, 70, 80, 90, 100 ] }
Syntax:
{
$pullAll: {<array>: [value1, value2, value3, ...]}
}
eg:
> db.students.insert({_id:8,
marks:[10,20,10,10,20,20,10,30,30,40,50,60]})
> db.students.update({_id:8},{$pullAll: {marks: [10,20,30,40]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{ "_id" : 8, "marks" : [ 50, 60 ] }
168
2. $[]
3. $[element]
4. $push operators with modifiers: $each, $position, $sort, $slice
5. $addToSet operator with $each modifier
6. $pop
7. $pull
8. $pullAll
CRUD Operations:
C --->Create|Insert
R --->Retrieve | Read
U --->Update
D --->Delete
1. deleteOne()
2. deleteMany()
3. remove()
1. deleteOne():
---------------
To delete only one document that matches the query criteria.
> db.collection.deleteOne({query})
2. deleteMany():
----------------
To delete all matched documents that matches query criteria.
169
> db.collection.deleteMany({query})
Case Study:
-----------
db.employees.insert({_id:1,eno:100,ename:"Sunny",esal:1000,eaddr:"M
umbai"})
db.employees.insert({_id:2,eno:200,ename:"Bunny",esal:2000,eaddr:"H
yderabad"})
db.employees.insert({_id:3,eno:300,ename:"Chinny",esal:3000,eaddr:"M
umbai"})
db.employees.insert({_id:4,eno:400,ename:"Vinny",esal:4000,eaddr:"De
lhi"})
db.employees.insert({_id:5,eno:500,ename:"Pinny",esal:5000,eaddr:"Ch
ennai"})
db.employees.insert({_id:6,eno:600,ename:"Tinny",esal:6000,eaddr:"Mu
mbai"})
db.employees.insert({_id:7,eno:700,ename:"Zinny",esal:7000,eaddr:"De
lhi"})
170
{ "acknowledged" : true, "deletedCount" : 3 }
Q4. Delete all documents where esal is greater than 5000 and eaddr is
Delhi?
1st way:
-------
> db.employees.deleteMany({$and: [{esal: {$gt: 5000}},{eaddr:
"Delhi"}]})
{ "acknowledged" : true, "deletedCount" : 1 }
2nd way:
-------
db.employees.deleteMany({esal: {$gt: 5000}, eaddr: "Delhi"})
Q5. How to delete all documents from the collection without deleting
collection?
> db.employees.deleteMany({})
> db.employees.find().count()
6
> db.employees.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 6 }
> db.employees.find().count()
0
> show collections
employees
students
3. remove() :
171
-------------
We can use remove() method to delete either a single document or
multiple documents.
Syntax:
> db.collection.remove({query},justOne)
172
students
db.collection.drop()
173
db.collection.deleteMany({})
Ans:
db.collection.drop() --->Both collection and documents will be deleted.
db.collection.deleteMany({}) --->Only documents will be deleted but not
collection.
> db.collection.deleteMany({})
> db.collection.remove({})
> db.collection.remove({},false)
All are equal and deleting all documents from the collection.
174
https://www.mongodb.com/try/download/database-tools
Download
we will get zip file: mongodb-database-tools-windows-x86_64-100.3.0
Extract this zip file and copy the utility tools to our mongodb bin
folder.
C:\Program Files\MongoDB\Server\4.4\bin
1. mongoimport:
---------------
We can use this tool to import data into mongodb database from the
files like json file, csv file etc
eg:
mongoimport -d durgadb -c students --file students.json --jsonArray
2. mongoexport:
---------------
We can use this tool to export specific data from the given collection
to the files.
175
The data will be stored in the file in json format.
Syntax:
mongoexport -d databaseName -c collectionName -o fileName
-d ===>databaseName
-c ===>collectionName
-o ===>Name of the file where exported data sholud be written.
emp.txt:
-------
{"_id":{"$oid":"5fe220cd573f5ff261265825"},"eno":100.0,"ename":"Sunn
y","esal":1000.0,"eaddr":"Mumbai"}
{"_id":{"$oid":"5fe221b6573f5ff261265826"},"eno":200.0,"ename":"Bunn
y","esal":2000.0,"eaddr":"Hyd"}
{"_id":{"$oid":"5fe221b6573f5ff261265827"},"eno":300.0,"ename":"Chin
ny","esal":3000.0,"eaddr":"Chennai"}
{"_id":{"$oid":"5fe221b6573f5ff261265828"},"eno":400.0,"ename":"Vinn
y","esal":4000.0,"eaddr":"Delhi"}
{"_id":{"$oid":"5fe222ac573f5ff261265829"},"eno":777.0,"ename":"Sunn
y","esal":1000.0,"eaddr":"Mumbai"}
{"_id":{"$oid":"5fe222e9573f5ff26126582a"},"eno":888.0,"ename":"Bunn
y","esal":2000.0,"eaddr":"Hyd"}
176
{"_id":{"$oid":"5fe222e9573f5ff26126582b"},"eno":999.0,"ename":"Chin
ny","esal":3000.0,"eaddr":"Chennai"}
{"_id":{"$oid":"5fe2241b573f5ff26126582c"},"eno":77777.0,"ename":"Sa
chin","esal":99999.0,"eaddr":"Mumbai"}
C:\Users\lenovo\Desktop>mongodump
2021-02-14T09:32:01.762+0530 writing admin.system.version to
dump\admin\system.version.bson
2021-02-14T09:32:01.764+0530 done dumping admin.system.version
(1 document)
2021-02-14T09:32:01.765+0530 writing durgadb.employees3 to
dump\durgadb\employees3.bson
2021-02-14T09:32:01.773+0530 done dumping durgadb.employees3
(1 document)
2021-02-14T09:32:01.773+0530 writing durgadb.employees2 to
dump\durgadb\employees2.bson
2021-02-14T09:32:01.775+0530 done dumping durgadb.employees2
(0 documents)
2021-02-14T09:32:01.776+0530 writing studentdb.sssdb1 to
dump\studentdb\sssdb1.bson
177
2021-02-14T09:32:01.777+0530 done dumping studentdb.sssdb1 (0
documents)
2021-02-14T09:32:02.067+0530 writing durgadb.employees to
dump\durgadb\employees.bson
2021-02-14T09:32:02.067+0530 writing studentdb.sssdb2 to
dump\studentdb\sssdb2.bson
2021-02-14T09:32:02.067+0530 writing studentdb.students to
dump\studentdb\students.bson
2021-02-14T09:32:02.070+0530 done dumping studentdb.students (1
document)
2021-02-14T09:32:02.075+0530 done dumping studentdb.sssdb2 (1
document)
2021-02-14T09:32:02.075+0530 done dumping durgadb.employees (8
documents)
Note: dump folder got created which contains data from all databases.
For every collection 2 files will be created.
bson file==>contains original data in bson format
json file==>contains metadata related to that collection.
C:\Users\lenovo\Desktop>tree /f dump
Folder PATH listing
Volume serial number is 00000220 526A:E31B
C:\USERS\LENOVO\DESKTOP\DUMP
├───admin
│ system.version.bson
│ system.version.metadata.json
│
├───durgadb
│ employees.bson
│ employees.metadata.json
│ employees2.bson
│ employees2.metadata.json
178
│ employees3.bson
│ employees3.metadata.json
│
└───studentdb
sssdb1.bson
sssdb1.metadata.json
sssdb2.bson
sssdb2.metadata.json
students.bson
students.metadata.json
Note:
The original data stored in bson form where as metadata stored in json
form.
But we can convert bson data to json format by using bsondump tool.
eg-1: Convert bson data from employees.bson to json form and display
to the console.
C:\Users\lenovo\Desktop\dump\durgadb>bsondump employees.bson
{"_id":{"$oid":"5fe220cd573f5ff261265825"},"eno":{"$numberDouble":"1
00.0"},"ename":"Sunny","esal":{"$numberDouble":"1000.0"},"eaddr":"M
umbai"}
{"_id":{"$oid":"5fe221b6573f5ff261265826"},"eno":{"$numberDouble":"2
00.0"},"ename":"Bunny","esal":{"$numberDouble":"2000.0"},"eaddr":"Hy
d"}
{"_id":{"$oid":"5fe221b6573f5ff261265827"},"eno":{"$numberDouble":"3
00.0"},"ename":"Chinny","esal":{"$numberDouble":"3000.0"},"eaddr":"C
hennai"}
{"_id":{"$oid":"5fe221b6573f5ff261265828"},"eno":{"$numberDouble":"4
00.0"},"ename":"Vinny","esal":{"$numberDouble":"4000.0"},"eaddr":"Del
hi"}
179
{"_id":{"$oid":"5fe222ac573f5ff261265829"},"eno":{"$numberDouble":"7
77.0"},"ename":"Sunny","esal":{"$numberDouble":"1000.0"},"eaddr":"M
umbai"}
{"_id":{"$oid":"5fe222e9573f5ff26126582a"},"eno":{"$numberDouble":"8
88.0"},"ename":"Bunny","esal":{"$numberDouble":"2000.0"},"eaddr":"Hy
d"}
{"_id":{"$oid":"5fe222e9573f5ff26126582b"},"eno":{"$numberDouble":"9
99.0"},"ename":"Chinny","esal":{"$numberDouble":"3000.0"},"eaddr":"C
hennai"}
{"_id":{"$oid":"5fe2241b573f5ff26126582c"},"eno":{"$numberDouble":"7
7777.0"},"ename":"Sachin","esal":{"$numberDouble":"99999.0"},"eaddr
":"Mumbai"}
2021-02-14T09:41:01.199+0530 8 objects found
eg-2: Convert bson data from employees.bson to json form and write to
emp.json file
Bydefault bsondump tool outputs data to the standard output(console).
We can write converted data to the files also. For this we have to use -
-outFile argument.
C:\Users\lenovo\Desktop\dump\durgadb>bsondump --outFile=emp.json
employees.bson
2021-02-14T09:44:29.662+0530 8 objects found
emp.json:
---------
{"_id":{"$oid":"5fe220cd573f5ff261265825"},"eno":{"$numberDouble":"1
00.0"},"ename":"Sunny","esal":{"$numberDouble":"1000.0"},"eaddr":"M
umbai"}
180
{"_id":{"$oid":"5fe221b6573f5ff261265826"},"eno":{"$numberDouble":"2
00.0"},"ename":"Bunny","esal":{"$numberDouble":"2000.0"},"eaddr":"Hy
d"}
{"_id":{"$oid":"5fe221b6573f5ff261265827"},"eno":{"$numberDouble":"3
00.0"},"ename":"Chinny","esal":{"$numberDouble":"3000.0"},"eaddr":"C
hennai"}
{"_id":{"$oid":"5fe221b6573f5ff261265828"},"eno":{"$numberDouble":"4
00.0"},"ename":"Vinny","esal":{"$numberDouble":"4000.0"},"eaddr":"Del
hi"}
{"_id":{"$oid":"5fe222ac573f5ff261265829"},"eno":{"$numberDouble":"7
77.0"},"ename":"Sunny","esal":{"$numberDouble":"1000.0"},"eaddr":"M
umbai"}
{"_id":{"$oid":"5fe222e9573f5ff26126582a"},"eno":{"$numberDouble":"8
88.0"},"ename":"Bunny","esal":{"$numberDouble":"2000.0"},"eaddr":"Hy
d"}
{"_id":{"$oid":"5fe222e9573f5ff26126582b"},"eno":{"$numberDouble":"9
99.0"},"ename":"Chinny","esal":{"$numberDouble":"3000.0"},"eaddr":"C
hennai"}
{"_id":{"$oid":"5fe2241b573f5ff26126582c"},"eno":{"$numberDouble":"7
7777.0"},"ename":"Sachin","esal":{"$numberDouble":"99999.0"},"eaddr
":"Mumbai"}
mongoimport
mongoexport
mongodump
bson format
bsondump
181
----------------------------------------------------------------------
We have to use mongorestore command without any arguments.
C:\Users\lenovo\Desktop>mongorestore
2021-02-15T09:22:32.927+0530 using default 'dump' directory
2021-02-15T09:22:32.928+0530 preparing collections to restore from
2021-02-15T09:22:32.930+0530 reading metadata for
durgadb.employees from dump\durgadb\employees.metadata.json
2021-02-15T09:22:32.930+0530 reading metadata for
studentdb.students from dump\studentdb\students.metadata.json
2021-02-15T09:22:32.930+0530 reading metadata for
durgadb.employees3 from dump\durgadb\employees3.metadata.json
2021-02-15T09:22:33.232+0530 restoring studentdb.students from
dump\studentdb\students.bson
2021-02-15T09:22:33.232+0530 reading metadata for
studentdb.sssdb2 from dump\studentdb\sssdb2.metadata.json
2021-02-15T09:22:33.243+0530 no indexes to restore
2021-02-15T09:22:33.243+0530 finished restoring studentdb.students
(1 document, 0 failures)
2021-02-15T09:22:33.243+0530 reading metadata for
studentdb.sssdb1 from dump\studentdb\sssdb1.metadata.json
2021-02-15T09:22:33.756+0530 restoring durgadb.employees3 from
dump\durgadb\employees3.bson
2021-02-15T09:22:33.767+0530 no indexes to restore
2021-02-15T09:22:33.767+0530 finished restoring
durgadb.employees3 (1 document, 0 failures)
2021-02-15T09:22:33.767+0530 reading metadata for
durgadb.employees2 from dump\durgadb\employees2.metadata.json
2021-02-15T09:22:33.823+0530 restoring durgadb.employees from
dump\durgadb\employees.bson
2021-02-15T09:22:33.840+0530 no indexes to restore
2021-02-15T09:22:33.840+0530 finished restoring
durgadb.employees (8 documents, 0 failures)
182
2021-02-15T09:22:33.934+0530 restoring studentdb.sssdb2 from
dump\studentdb\sssdb2.bson
2021-02-15T09:22:33.952+0530 no indexes to restore
2021-02-15T09:22:33.952+0530 finished restoring studentdb.sssdb2
(1 document, 0 failures)
2021-02-15T09:22:34.012+0530 restoring studentdb.sssdb1 from
dump\studentdb\sssdb1.bson
2021-02-15T09:22:34.026+0530 no indexes to restore
2021-02-15T09:22:34.026+0530 finished restoring studentdb.sssdb1
(0 documents, 0 failures)
2021-02-15T09:22:34.176+0530 restoring durgadb.employees2 from
dump\durgadb\employees2.bson
2021-02-15T09:22:34.192+0530 no indexes to restore
2021-02-15T09:22:34.192+0530 finished restoring
durgadb.employees2 (0 documents, 0 failures)
2021-02-15T09:22:34.192+0530 11 document(s) restored
successfully. 0 document(s) failed to restore.
183
2021-02-15T09:30:29.058+0530 writing durgadb.employees2 to
dump\durgadb\employees2.bson
2021-02-15T09:30:29.060+0530 writing durgadb.employees to
dump\durgadb\employees.bson
2021-02-15T09:30:29.061+0530 done dumping durgadb.employees2
(0 documents)
2021-02-15T09:30:29.062+0530 done dumping durgadb.employees (8
documents)
C:\Users\lenovo\Desktop>tree /f dump
Folder PATH listing
Volume serial number is 000001A3 526A:E31B
C:\USERS\LENOVO\DESKTOP\DUMP
└───durgadb
employees.bson
employees.metadata.json
employees2.bson
employees2.metadata.json
employees3.bson
employees3.metadata.json
184
2021-02-15T09:34:35.988+0530 reading metadata for
durgadb.employees from dump\durgadb\employees.metadata.json
2021-02-15T09:34:35.989+0530 reading metadata for
durgadb.employees3 from dump\durgadb\employees3.metadata.json
2021-02-15T09:34:35.989+0530 reading metadata for
durgadb.employees2 from dump\durgadb\employees2.metadata.json
2021-02-15T09:34:36.172+0530 restoring durgadb.employees3 from
dump\durgadb\employees3.bson
2021-02-15T09:34:36.184+0530 no indexes to restore
2021-02-15T09:34:36.184+0530 finished restoring
durgadb.employees3 (1 document, 0 failures)
2021-02-15T09:34:36.556+0530 restoring durgadb.employees from
dump\durgadb\employees.bson
2021-02-15T09:34:36.568+0530 no indexes to restore
2021-02-15T09:34:36.568+0530 finished restoring
durgadb.employees (8 documents, 0 failures)
2021-02-15T09:34:36.661+0530 restoring durgadb.employees2 from
dump\durgadb\employees2.bson
2021-02-15T09:34:36.680+0530 no indexes to restore
2021-02-15T09:34:36.680+0530 finished restoring
durgadb.employees2 (0 documents, 0 failures)
2021-02-15T09:34:36.680+0530 9 document(s) restored successfully.
0 document(s) failed to restore.
185
2021-02-15T09:37:26.592+0530 writing durgadb.employees to
dump\durgadb\employees.bson
2021-02-15T09:37:26.594+0530 done dumping durgadb.employees (8
documents)
C:\Users\lenovo\Desktop>tree /f dump
Folder PATH listing
Volume serial number is 000001B4 526A:E31B
C:\USERS\LENOVO\DESKTOP\DUMP
└───durgadb
employees.bson
employees.metadata.json
186
Note: We can take dump and restore all databases or a particular
database or a particular collection.
Summary:
--------
1. mongoimport --->To import data from the files
2. mongoexport --->To export mongodb data to the files
3. mongodump --->To create dump for database
4. bsondump --->To convert bson data into json data
5. mongorestore --->To restore data from the dump.
Q. Sir, one last question: I want to know how many databases contain
"books" collection. How can I perform a database level search from
mongo shell?
python script:
show dbs
use every db
show collections
if books collection is there then print database name
187
3. No help tips
4. Readability is not up to the mark.
5. No coloring
6. Operations will become very complex
7. Not that much convenient to use
etc
1. Robo 3T:
-----------
The Robo 3T (formerly Robomongo) tool has been acquired by 3T
Software Labs, the creators of the MongoDB client Studio 3T (formerly
MongoChef).
It is freeware and lightweight GUI tool for MongoDB operations.
website: robomongo.org
Two tools: Robo 3T and Studio 3T
Robo 3T is freeware and Studio 3T is licensed.
Download: studio-3t-robo-3t-windows-double-pack.zip
188
2. Studio 3T:
-------------
Formerly MongoChef.
The most popular DB Tool for MongoDB operations.
https://robomongo.org/
studio-3t-x64.exe
mongodbmanagerfree_inst.exe
Features
Full support for MongoDB and MongoDB Enterprise versions from 2.2 to
4.4
Certified on MongoDB Enterprise. Supports Kerberos, LDAP and
MONGODB-X509 authentications
Fully functional MongoDB UI Shell with code autocompletion, syntax
highlighting and hints
Fully compatible with Amazon DocumentDB and MongoDB on Azure
Cosmos DB
189
Support of replica sets, standalone hosts and sharded clusters
connections
Easy-to-use document viewer and editor with Tree, Table and JSON
view modes
Simple view and management of all MongoDB objects: databases,
collections, views, indices, users, roles and functions
SSH tunneling for MongoDB connections
Performance monitoring tools
Import tables from MySQL and SQL Server databases
Export documents to CSV, XML, JSON and XLSX file formats
Import documents from JSON and CSV files
LINQ Query Tool
4. MongoDB Compass:
-------------------
https://www.mongodb.com/products/compass
Connection String:
mongodb://localhost:27017
MongoDB Indexing:
----------------
190
find()
update({},{})
delete({})
goto chapter11
If i need to run only one query from very large data, then do we need to
create index as it will also takes time
Book my show
select all movies in given theatre and in a particular city
IRCTC
100000 trains
Hyderabad
191
db.employees.find({ename: "Sunny"}).pretty()
db.employees.find({ename: "Sunny"}).explain("executionStats")
192
"filter" : {
"ename" : {
"$eq" : "Sunny"
}
},
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 15,
"advanced" : 2,
"needTime" : 12,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"direction" : "forward",
"docsExamined" : 13
}
},
"serverInfo" : {
"host" : "DESKTOP-ECE8V3R",
"port" : 27017,
"version" : "4.4.2",
"gitVersion" :
"15e73dc5738d2278b688f8929aee605fe4279b0e"
},
"ok" : 1
}
193
> db.employees.getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
db.employees.createIndex({ename: 1})
> db.employees.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"ename" : 1
},
"name" : "ename_1"
}
]
194
> db.employees.find({ename: "Sunny"}).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "durgadb.employees",
"indexFilterSet" : false,
"parsedQuery" : {
"ename" : {
"$eq" : "Sunny"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"ename" : 1
},
"indexName" : "ename_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"ename" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"ename" : [
"[\"Sunny\", \"Sunny\"]"
]
}
195
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 0,
"totalKeysExamined" : 2,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 2,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
196
"isEOF" : 1,
"keyPattern" : {
"ename" : 1
},
"indexName" : "ename_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"ename" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"ename" : [
"[\"Sunny\", \"Sunny\"]"
]
},
"keysExamined" : 2,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
},
"serverInfo" : {
"host" : "DESKTOP-ECE8V3R",
"port" : 27017,
"version" : "4.4.2",
"gitVersion" :
"15e73dc5738d2278b688f8929aee605fe4279b0e"
},
197
"ok" : 1
}
198
As the number of documents to be scan, is reduces and hence
performance will be improved.
199
executionStats – this mode includes all the information provided by
the queryPlanner, plus the statistics. Statistics include details such as
the number of documents examined and returned, the execution time
in milliseconds, and so on.
db.collection.find({query}).explain("executionStats")
db.employees.getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
Aggregation Framework:
----------------------
db.employees.insertOne({eno:100,ename:"Sunny",esal:1000,eaddr:"Mu
mbai"})
db.employees.insertOne({eno:200,ename:"Bunny",esal:2000,eaddr:"Hy
derabad"})
db.employees.insertOne({eno:300,ename:"Chinny",esal:3000,eaddr:"Hy
derabad"})
db.employees.insertOne({eno:400,ename:"Vinny",esal:4000,eaddr:"Mu
mbai"})
db.employees.insertOne({eno:500,ename:"Pinny",esal:5000,eaddr:"Che
nnai"})
db.employees.insertOne({eno:600,ename:"Zinny",esal:6000,eaddr:"Che
nnai"})
db.employees.insertOne({eno:700,ename:"Yinny",esal:7000,eaddr:"Hyd
erabad"})
200
db.employees.find().pretty()
db.employees.find({eaddr:"Hyderabad"}).pretty()
aggregate() vs find():
----------------------
aggregate() method can perform some processing and provide results
in our customized format.
But find() method will always provide data as it is without performing
any processing and in the existing format only.
db.employees.aggregate([
201
{ $group: {_id:null,totalsalary:{$sum:"$esal"}}}
])
Note:
$group stage:
------------
1. It is the most important stage.
2. It can be used to group documents based on required fields. It
merges different documents into new documents.
eg: group all documents city wise
group all documents department wise
3. The first parameter in $group stage is always _id.
4. We should use _id to specify field based on which we have to
perform grouping. ie _id defines by which fields we can group.
5. If we want to process all records then we have to provide null value
to _id field.
6. $group is exactly same as group by phrase in relational databases.
Accumulator operators:
----------------------
These operators can be used for accumulation purpose.
The following various accumulator operators:
1. $sum: Returns a sum of numerical values. Ignores non-numeric
values.
2. $avg: Returns an average of numerical values. Ignores non-numeric
values.
3. $max: Returns the highest expression value for each group.
4. $min: Returns the lowest expression value for each group.
etc
202
db.employees.aggregate([
{ $group: {_id:null,averagesalary:{$avg:"$esal"}}}
])
db.employees.aggregate([
{$group: {_id:"$eaddr",maxSalary:{$max:"$esal"}}}
])
o/p:
{ "_id" : "Mumbai", "maxSalary" : 4000 }
{ "_id" : "Hyderabad", "maxSalary" : 7000 }
{ "_id" : "Chennai", "maxSalary" : 6000 }
203
db.employees.aggregate([
{$group: {_id:"$eaddr",totalSalary:{$sum:"$esal"}}}
])
o/p:
{ "_id" : "Mumbai", "totalSalary" : 5000 }
{ "_id" : "Hyderabad", "totalSalary" : 12000 }
{ "_id" : "Chennai", "totalSalary" : 11000 }
o/p:
{ "_id" : "Mumbai", "averageSalary" : 2500 }
{ "_id" : "Hyderabad", "averageSalary" : 4000 }
{ "_id" : "Chennai", "averageSalary" : 5500 }
204
o/p:
{ "_id" : "Mumbai", "employeecount" : 2 }
{ "_id" : "Hyderabad", "employeecount" : 3 }
{ "_id" : "Chennai", "employeecount" : 2 }
Aggregation Pipeline:
---------------------
We can define multiple stages in the aggregation and all these stages
will form pipeline, which is known as aggregation pipeline.
db.collection.aggregate([
{stage-1},
{stage-2},
{stage-3},
{stage-4},
{stage-5}
...
])
2. $sort stage:
---------------
205
It sorts all input documents and returns them to the pipeline in sorted
order.
db.employees.aggregate([
{ $group: {_id:"$eaddr",totalSalary:{$sum:"$esal"}}},
{ $sort:{totalSalary: -1}}
])
o/p:
{ "_id" : "Hyderabad", "totalSalary" : 12000 }
{ "_id" : "Chennai", "totalSalary" : 11000 }
{ "_id" : "Mumbai", "totalSalary" : 5000 }
db.employees.aggregate([
{ $group: {_id:"$eaddr", employeeCount: {$sum:1}}},
{ $sort: {_id:1}}
])
o/p:
206
{ "_id" : "Chennai", "employeeCount" : 2 }
{ "_id" : "Hyderabad", "employeeCount" : 3 }
{ "_id" : "Mumbai", "employeeCount" : 2 }
3. $project stage:
------------------
By using this $project stage, we can restrict documents with our
required fields only. Here we can include new fields also. With the
existing field values we can create new fields also.
eg: first name and last name fields are there. We can combined these
fields with just name field.
Syntax:
{ $project: { field:0|1 } }
o/p:
{ "totalSalary" : 28000 }
eg-2: Find city wise total salary and city name should be in uppercase
and sort the documents in ascending order of salaries?
db.employees.aggregate([
{$group: {_id:"$eaddr",totalSalary:{$sum:"$esal"}}},
207
{$project: {_id:0,city:{$toUpper:"$_id"},totalSalary:1}},
{$sort: {totalSalary: 1}}
])
o/p:
{ "totalSalary" : 5000, "city" : "MUMBAI" }
{ "totalSalary" : 11000, "city" : "CHENNAI" }
{ "totalSalary" : 12000, "city" : "HYDERABAD" }
db.employees.aggregate([
{$group: {_id:"$eaddr",totalSalary:{$sum:"$esal"}}},
{$project: {_id:0,city:{$concat:["$_id"," ","City"]},totalSalary:1}},
{$sort: {totalSalary: 1}}
])
o/p:
{ "totalSalary" : 5000, "city" : "Mumbai City" }
{ "totalSalary" : 11000, "city" : "Chennai City" }
{ "totalSalary" : 12000, "city" : "Hyderabad City" }
$group vs $project:
-------------------
If we want to group multiple documents into a single document then
we should use $group stage.
eg: $sum,$avg,$max etc
4. $match stage:
----------------
208
To filter documents based on required condition. It is exactly same as
find() method <query>.
Syntax:
{ $match: { <query> } }
eg: To find the number of employees whose salary greater than 1500.
Find such employees count citywise. Display documents in ascending
order of employee count.
db.employees.aggregate([
{$match: {esal:{$gt: 1500}}},
{$group: {_id:"$eaddr",employeeCount:{$sum:1}}},
{$sort: {employeeCount:1}}
])
o/p:
{ "_id" : "Mumbai", "employeeCount" : 1 }
{ "_id" : "Chennai", "employeeCount" : 2 }
{ "_id" : "Hyderabad", "employeeCount" : 3 }
209