SlideShare a Scribd company logo
MongoDB Basic Shell Program’s
CRUD
Operations for
MongoDB
Start the Shell
• C:mongodb-win32-x86_64-
2.2.3bin>mongo.exe
MongoDB shell version: 2.2.3
connecting to: test
Create Document the insert()
> register={"uName":"anish",
... "date":new Date()}
{ "uName" : "anish", "date" : ISODate("2013-03-10T10:08:30.891Z") }
> db.user.insert(register)
> db.user.find()
{ "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname" :
"anish", "date" : ISODate("2013-03-10T09:52:53.144Z") }
{ "_id" : ObjectId("513c5be21b37fce6f69eb9e2"), "uName" :
"anish", "date" : ISODate("2013-03-10T10:08:30.891Z") }
The “_id” field If you attempt to insert a document without
the _id field, the client library or the mongod instance will add
an _id field and populate the field with a unique ObjectId.
Create Document the insert() with
own “_id” key
> register =
{"_id":100,"uName":"arjun","date":new Date()}
{
"_id" : 100,
"uName" : "arjun",
"date" : ISODate("2013-03-12T14:41:15.124Z")
}
>db.user.insert(register)
> db.user.find({"uName":"arjun"})
{ "_id" : 100, "uName" : "arjun", "date" :
ISODate("2013-03-12T14:41:15.124Z") }
Create Document the insert() same “_id” key
will throw E11000 duplicate key error
> register =
{"_id":100,"uName":"arjun","date":new Date()}
{
"_id" : 100,
"uName" : "arjun",
"date" : ISODate("2013-03-12T14:44:14.163Z")
}
> db.user.insert(register)
E11000 duplicate key error index: test.user.$_id_
dup key: { : 100.0 }
MongoDB Reading/finding
Document
Reading/finding Document Use Case
detailed
Reading/finding in Document
Syntax
db.collection.find( <query>,
<projection> )
<query> argument corresponds to
the WHERE statement, and
<projection> argument corresponds
to the list of fields to select from the
result set.
Reading Document
> db.user.findOne()
{
"_id" : ObjectId("513c581f1b37fce6f69eb9e0"),
"fname" : "anish",
"date" : ISODate("2013-03-10T09:52:53.144Z")
}
> db.user.find()
{ "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname"
: "anish", "date" : ISODate("2013-03-10T09:52:53.144Z") }
{ "_id" : ObjectId("513c5be21b37fce6f69eb9e2"),
"uName" : "anish", "date" : ISODate("2013-03-
10T10:08:30.891Z") }
Refer more use case on find() on the Other video
findOne()
Find the first record in the document
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"remarks" : [
{
"name" : "anish",
"rate" : "good"
},
{
"name" : "nath",
"rate" : "bad"
}
],
"uName" : "anish"
}
findOne() : specify which key to return
• Find the first record in the document and return only “_id”
> db.user.findOne({},{"_id":1})
{ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d") }
• Find the first record in the document and return only
“_id” and date
> db.user.findOne({},{"_id":1,"date":1})
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"date" : ISODate("2013-03-10T11:30:50.555Z")
}
• Find the first record in the document and return all the
data except the “_id” field
> db.user.findOne({},{"_id":0})
{
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"remarks" : [
{
"name" : "anish",
"rate" : "good"
},
{
"name" : "nath",
"rate" : "bad"
}
],
"uName" : "anish"
}
finding in Subdocument
>db.user.find(
{"remarks.name":"anish" })
{ "_id" :
ObjectId("513c6ef1cfce9090d3fd8b1d"),
"date" : ISODate("2013-03-
10T11:30:50.555Z"), "remarks" : [ {
"name
" : "anish", "rate" : "good" }, {
"name" : "nath", "rate" : "bad" } ],
"uName" : "anish" }
findOne()/find()
• Summary on <K,boolean>
specify <“k”,0> or <“K”,1>
to show/hide data to be
returned from the
collection
MongoDB updating Document
Updating Document Use Case
detailed
Updating Document <K,V>
> db.user.find()
{ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "nath", "date" :
ISODate("2013-03-10T11:30:50.555Z") }
Update the uName with “anish”
Step 1 find userName with “nath”
> var test=db.user.findOne({"uName":"nath"})
Step 2
> test.uName="anish"
anish
Step 3
>db.user.update({"uName":"nath"},test)
> db.user.findOne()
{ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish", "date" : ISODate("2013-03-
10T11:30:50.555Z") }
Updating Document
Syntax : db.collection.update( <query>,<update>,
<options> )
> db.user.find()
{ "_id" : ObjectId("513c60deb2080f893286d0a3"), "uName" : "anish", "date" : ISODate("2013-03-
10T10:30:47.033Z") }
{ "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03-
10T10:31:18.632Z") }
> register.uName={"uName" : "nath"}
{ "uName" : "nath" }
> db.user.update({"uName":"anish"},register)
> db.user.find()
{ "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03-
10T10:31:18.632Z") }
{ "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03-
10T10:31:18.632Z"), "uName" : {
"uName" : "nath" } }
Updating Document adding a new <k,V> to the document
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "nath",
"date" : ISODate("2013-03-10T11:30:50.555Z")
}
Add a age in the document i.e
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 32
}
Step 1 : findById
var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")})
Step2 : add age to the document
> findById.age=32
32
Step 3 Update the Document
> db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")},findById)
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 32
}
Updating Document :Add a new Structure in the existing Document
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 32
}
Change the Document to the following
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 32,
"family" : {
"motherName" : "Indu",
"wifeName" : "manisha",
"fatherName" : "Anil"
}
}
Updating Document :Add a new Structure in the existing Document
Step 1 : findUserById
>var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")})
Step 2: Add a family JSON entry
>findById.family={"motherName":"Indu","wifeName":"manisha","fatherName":"A
nil"}
{ "motherName" : "Indu", "wifeName" : "manisha", "fatherName" : "Anil" }
Step 3 : Update the docuemt :
> db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")},findById)
Step 4 : Search the User DB
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 32,
"family" : {
"motherName" : "Indu",
"wifeName" : "manisha",
"fatherName" : "Anil"
}
}
Deleting Document
> db.user.find()
{ "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03-
10T10:31:18.632Z") }
{ "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03-
10T10:31:18.632Z"), "uName" : {
"uName" : "nath" } }
>
db.user.remove({"_id":ObjectId("513c60fab2080f893286d0a4")}
)
> db.user.find()
{ "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03-
10T10:31:18.632Z"), "uName" : {
"uName" : "nath" } }
>db.user.remove() : deletes documents
permanently from the database
Thanks for Watching
feedback
appreciated………………
…………

More Related Content

What's hot (20)

PDF
Mongo db for C# Developers
Simon Elliston Ball
 
PDF
Mongo db for c# developers
Simon Elliston Ball
 
PDF
Data analysis and visualization with mongo db [mongodb world 2016]
Alexander Hendorf
 
PDF
Mongodb index 讀書心得
cc liu
 
PDF
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
 
PDF
MongoD Essentials
zahid-mian
 
PPTX
Mythbusting: Understanding How We Measure the Performance of MongoDB
MongoDB
 
PDF
Softshake - Offline applications
jeromevdl
 
PDF
Sebastian Schmidt, Rachel Myers - How To Go Serverless And Not Violate The GD...
Codemotion
 
PDF
Optimizing Slow Queries with Indexes and Creativity
MongoDB
 
PDF
The Ring programming language version 1.5.1 book - Part 42 of 180
Mahmoud Samir Fayed
 
PDF
MongoDB全機能解説2
Takahiro Inoue
 
PDF
MongoDB World 2019: Using Client Side Encryption in MongoDB 4.2 Link
MongoDB
 
PDF
MongoDBで作るソーシャルデータ新解析基盤
Takahiro Inoue
 
PPTX
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
MongoDB
 
PDF
MongoDB .local Munich 2019: New Encryption Capabilities in MongoDB 4.2: A Dee...
MongoDB
 
KEY
Geospatial Indexing and Querying with MongoDB
Grant Goodale
 
PDF
Introduction to python
Hyun-hwan Jeong
 
PDF
San Francisco Java User Group
kchodorow
 
KEY
Mapping Flatland: Using MongoDB for an MMO Crossword Game (GDC Online 2011)
Grant Goodale
 
Mongo db for C# Developers
Simon Elliston Ball
 
Mongo db for c# developers
Simon Elliston Ball
 
Data analysis and visualization with mongo db [mongodb world 2016]
Alexander Hendorf
 
Mongodb index 讀書心得
cc liu
 
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
 
MongoD Essentials
zahid-mian
 
Mythbusting: Understanding How We Measure the Performance of MongoDB
MongoDB
 
Softshake - Offline applications
jeromevdl
 
Sebastian Schmidt, Rachel Myers - How To Go Serverless And Not Violate The GD...
Codemotion
 
Optimizing Slow Queries with Indexes and Creativity
MongoDB
 
The Ring programming language version 1.5.1 book - Part 42 of 180
Mahmoud Samir Fayed
 
MongoDB全機能解説2
Takahiro Inoue
 
MongoDB World 2019: Using Client Side Encryption in MongoDB 4.2 Link
MongoDB
 
MongoDBで作るソーシャルデータ新解析基盤
Takahiro Inoue
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
MongoDB
 
MongoDB .local Munich 2019: New Encryption Capabilities in MongoDB 4.2: A Dee...
MongoDB
 
Geospatial Indexing and Querying with MongoDB
Grant Goodale
 
Introduction to python
Hyun-hwan Jeong
 
San Francisco Java User Group
kchodorow
 
Mapping Flatland: Using MongoDB for an MMO Crossword Game (GDC Online 2011)
Grant Goodale
 

Viewers also liked (20)

PPTX
Mongo db query docuement
zarigatongy
 
PPTX
Big data references
zarigatongy
 
PPTX
Mongo db pagination
zarigatongy
 
PPTX
Mongo db index
zarigatongy
 
PPTX
Mongo db readingdocumentusecases
zarigatongy
 
PPTX
Mongo db introduction
zarigatongy
 
PPTX
Mongo db datatypes
zarigatongy
 
PPTX
Dhcpv6 Tutorial Overview, DHCP for Ipv6 ,RFC 3315 - IETF
zarigatongy
 
PPTX
The Point to Point Protocol (PPP)
zarigatongy
 
PPTX
RADIUS- Packet Example/Vendors
zarigatongy
 
PPTX
Top 10 programming langauges crossed decades
zarigatongy
 
PPTX
Password Authentication Protocol
zarigatongy
 
PPTX
Regular Expression
zarigatongy
 
PPTX
(Aes )ADVANCED ENCRYPTION STANDARD
zarigatongy
 
PPTX
Quick sort demo
zarigatongy
 
PPTX
Jvm architecture Method Area
zarigatongy
 
PPTX
Meet C1
Missbrearley
 
DOCX
Críticas contra irán
Luis Fernando Cantoral Benavides
 
Mongo db query docuement
zarigatongy
 
Big data references
zarigatongy
 
Mongo db pagination
zarigatongy
 
Mongo db index
zarigatongy
 
Mongo db readingdocumentusecases
zarigatongy
 
Mongo db introduction
zarigatongy
 
Mongo db datatypes
zarigatongy
 
Dhcpv6 Tutorial Overview, DHCP for Ipv6 ,RFC 3315 - IETF
zarigatongy
 
The Point to Point Protocol (PPP)
zarigatongy
 
RADIUS- Packet Example/Vendors
zarigatongy
 
Top 10 programming langauges crossed decades
zarigatongy
 
Password Authentication Protocol
zarigatongy
 
Regular Expression
zarigatongy
 
(Aes )ADVANCED ENCRYPTION STANDARD
zarigatongy
 
Quick sort demo
zarigatongy
 
Jvm architecture Method Area
zarigatongy
 
Meet C1
Missbrearley
 
Críticas contra irán
Luis Fernando Cantoral Benavides
 
Ad

Similar to Basic crud operation (20)

PPTX
Back to Basics 2017 - Your First MongoDB Application
Joe Drumgoole
 
PDF
MongoDB With Style
Gabriele Lana
 
PPTX
Back to Basics, webinar 2: La tua prima applicazione MongoDB
MongoDB
 
PPTX
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
PDF
ENIB 2015 2016 - CAI Web S02E03 - Forge JS 2/4 - MongoDB and NoSQL
Horacio Gonzalez
 
PDF
ENIB 2015-2016 - CAI Web - S01E01- MongoDB and NoSQL
Horacio Gonzalez
 
PPTX
Back to basics Italian webinar 2 Mia prima applicazione MongoDB
MongoDB
 
PPTX
MongoDB - Back to Basics - La tua prima Applicazione
Massimo Brignoli
 
PPTX
How to leverage what's new in MongoDB 3.6
Maxime Beugnet
 
KEY
Schema Design with MongoDB
rogerbodamer
 
PDF
Starting with MongoDB
DoThinger
 
PPTX
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
PPTX
Back to Basics Webinar 2 - Your First MongoDB Application
Joe Drumgoole
 
PPTX
MongoDB-SESSION03
Jainul Musani
 
PDF
Intro to MongoDB and datamodeling
rogerbodamer
 
PPTX
Mongo db basic installation
Kishor Parkhe
 
PDF
Creating, Updating and Deleting Document in MongoDB
Wildan Maulana
 
PDF
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
KEY
Mongo db presentation
Julie Sommerville
 
KEY
Schema design
christkv
 
Back to Basics 2017 - Your First MongoDB Application
Joe Drumgoole
 
MongoDB With Style
Gabriele Lana
 
Back to Basics, webinar 2: La tua prima applicazione MongoDB
MongoDB
 
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
ENIB 2015 2016 - CAI Web S02E03 - Forge JS 2/4 - MongoDB and NoSQL
Horacio Gonzalez
 
ENIB 2015-2016 - CAI Web - S01E01- MongoDB and NoSQL
Horacio Gonzalez
 
Back to basics Italian webinar 2 Mia prima applicazione MongoDB
MongoDB
 
MongoDB - Back to Basics - La tua prima Applicazione
Massimo Brignoli
 
How to leverage what's new in MongoDB 3.6
Maxime Beugnet
 
Schema Design with MongoDB
rogerbodamer
 
Starting with MongoDB
DoThinger
 
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Back to Basics Webinar 2 - Your First MongoDB Application
Joe Drumgoole
 
MongoDB-SESSION03
Jainul Musani
 
Intro to MongoDB and datamodeling
rogerbodamer
 
Mongo db basic installation
Kishor Parkhe
 
Creating, Updating and Deleting Document in MongoDB
Wildan Maulana
 
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
Mongo db presentation
Julie Sommerville
 
Schema design
christkv
 
Ad

Recently uploaded (20)

PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PPTX
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
PPTX
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Integrating IIoT with SCADA in Oil & Gas A Technical Perspective.pdf
Rejig Digital
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Integrating IIoT with SCADA in Oil & Gas A Technical Perspective.pdf
Rejig Digital
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 

Basic crud operation

  • 1. MongoDB Basic Shell Program’s CRUD Operations for MongoDB
  • 2. Start the Shell • C:mongodb-win32-x86_64- 2.2.3bin>mongo.exe MongoDB shell version: 2.2.3 connecting to: test
  • 3. Create Document the insert() > register={"uName":"anish", ... "date":new Date()} { "uName" : "anish", "date" : ISODate("2013-03-10T10:08:30.891Z") } > db.user.insert(register) > db.user.find() { "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname" : "anish", "date" : ISODate("2013-03-10T09:52:53.144Z") } { "_id" : ObjectId("513c5be21b37fce6f69eb9e2"), "uName" : "anish", "date" : ISODate("2013-03-10T10:08:30.891Z") } The “_id” field If you attempt to insert a document without the _id field, the client library or the mongod instance will add an _id field and populate the field with a unique ObjectId.
  • 4. Create Document the insert() with own “_id” key > register = {"_id":100,"uName":"arjun","date":new Date()} { "_id" : 100, "uName" : "arjun", "date" : ISODate("2013-03-12T14:41:15.124Z") } >db.user.insert(register) > db.user.find({"uName":"arjun"}) { "_id" : 100, "uName" : "arjun", "date" : ISODate("2013-03-12T14:41:15.124Z") }
  • 5. Create Document the insert() same “_id” key will throw E11000 duplicate key error > register = {"_id":100,"uName":"arjun","date":new Date()} { "_id" : 100, "uName" : "arjun", "date" : ISODate("2013-03-12T14:44:14.163Z") } > db.user.insert(register) E11000 duplicate key error index: test.user.$_id_ dup key: { : 100.0 }
  • 7. Reading/finding in Document Syntax db.collection.find( <query>, <projection> ) <query> argument corresponds to the WHERE statement, and <projection> argument corresponds to the list of fields to select from the result set.
  • 8. Reading Document > db.user.findOne() { "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname" : "anish", "date" : ISODate("2013-03-10T09:52:53.144Z") } > db.user.find() { "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname" : "anish", "date" : ISODate("2013-03-10T09:52:53.144Z") } { "_id" : ObjectId("513c5be21b37fce6f69eb9e2"), "uName" : "anish", "date" : ISODate("2013-03- 10T10:08:30.891Z") } Refer more use case on find() on the Other video
  • 9. findOne() Find the first record in the document > db.user.findOne() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "date" : ISODate("2013-03-10T11:30:50.555Z"), "remarks" : [ { "name" : "anish", "rate" : "good" }, { "name" : "nath", "rate" : "bad" } ], "uName" : "anish" }
  • 10. findOne() : specify which key to return • Find the first record in the document and return only “_id” > db.user.findOne({},{"_id":1}) { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d") } • Find the first record in the document and return only “_id” and date > db.user.findOne({},{"_id":1,"date":1}) { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "date" : ISODate("2013-03-10T11:30:50.555Z") } • Find the first record in the document and return all the data except the “_id” field > db.user.findOne({},{"_id":0}) { "date" : ISODate("2013-03-10T11:30:50.555Z"), "remarks" : [ { "name" : "anish", "rate" : "good" }, { "name" : "nath", "rate" : "bad" } ], "uName" : "anish" }
  • 11. finding in Subdocument >db.user.find( {"remarks.name":"anish" }) { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "date" : ISODate("2013-03- 10T11:30:50.555Z"), "remarks" : [ { "name " : "anish", "rate" : "good" }, { "name" : "nath", "rate" : "bad" } ], "uName" : "anish" }
  • 12. findOne()/find() • Summary on <K,boolean> specify <“k”,0> or <“K”,1> to show/hide data to be returned from the collection
  • 13. MongoDB updating Document Updating Document Use Case detailed
  • 14. Updating Document <K,V> > db.user.find() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "nath", "date" : ISODate("2013-03-10T11:30:50.555Z") } Update the uName with “anish” Step 1 find userName with “nath” > var test=db.user.findOne({"uName":"nath"}) Step 2 > test.uName="anish" anish Step 3 >db.user.update({"uName":"nath"},test) > db.user.findOne() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "anish", "date" : ISODate("2013-03- 10T11:30:50.555Z") }
  • 15. Updating Document Syntax : db.collection.update( <query>,<update>, <options> ) > db.user.find() { "_id" : ObjectId("513c60deb2080f893286d0a3"), "uName" : "anish", "date" : ISODate("2013-03- 10T10:30:47.033Z") } { "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03- 10T10:31:18.632Z") } > register.uName={"uName" : "nath"} { "uName" : "nath" } > db.user.update({"uName":"anish"},register) > db.user.find() { "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03- 10T10:31:18.632Z") } { "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03- 10T10:31:18.632Z"), "uName" : { "uName" : "nath" } }
  • 16. Updating Document adding a new <k,V> to the document > db.user.findOne() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "nath", "date" : ISODate("2013-03-10T11:30:50.555Z") } Add a age in the document i.e { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "anish", "date" : ISODate("2013-03-10T11:30:50.555Z"), "age" : 32 } Step 1 : findById var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}) Step2 : add age to the document > findById.age=32 32 Step 3 Update the Document > db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")},findById) > db.user.findOne() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "anish", "date" : ISODate("2013-03-10T11:30:50.555Z"), "age" : 32 }
  • 17. Updating Document :Add a new Structure in the existing Document > db.user.findOne() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "anish", "date" : ISODate("2013-03-10T11:30:50.555Z"), "age" : 32 } Change the Document to the following { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "anish", "date" : ISODate("2013-03-10T11:30:50.555Z"), "age" : 32, "family" : { "motherName" : "Indu", "wifeName" : "manisha", "fatherName" : "Anil" } }
  • 18. Updating Document :Add a new Structure in the existing Document Step 1 : findUserById >var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}) Step 2: Add a family JSON entry >findById.family={"motherName":"Indu","wifeName":"manisha","fatherName":"A nil"} { "motherName" : "Indu", "wifeName" : "manisha", "fatherName" : "Anil" } Step 3 : Update the docuemt : > db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")},findById) Step 4 : Search the User DB > db.user.findOne() { "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "anish", "date" : ISODate("2013-03-10T11:30:50.555Z"), "age" : 32, "family" : { "motherName" : "Indu", "wifeName" : "manisha", "fatherName" : "Anil" } }
  • 19. Deleting Document > db.user.find() { "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03- 10T10:31:18.632Z") } { "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03- 10T10:31:18.632Z"), "uName" : { "uName" : "nath" } } > db.user.remove({"_id":ObjectId("513c60fab2080f893286d0a4")} ) > db.user.find() { "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03- 10T10:31:18.632Z"), "uName" : { "uName" : "nath" } } >db.user.remove() : deletes documents permanently from the database