100% found this document useful (2 votes)
5K views5 pages

Durga Mongo

This document discusses document databases and provides examples of CRUD operations in MongoDB. It begins by explaining that a database can contain multiple collections, each containing multiple documents. Documents are analogous to rows in a relational database. Examples are provided of inserting documents into a collection using different methods like insertOne, insertMany, and insert. The document also discusses finding, updating, and deleting documents. It provides examples of loading data from JavaScript and JSON files using the load() and mongoimport commands respectively.

Uploaded by

Amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
5K views5 pages

Durga Mongo

This document discusses document databases and provides examples of CRUD operations in MongoDB. It begins by explaining that a database can contain multiple collections, each containing multiple documents. Documents are analogous to rows in a relational database. Examples are provided of inserting documents into a collection using different methods like insertOne, insertMany, and insert. The document also discusses finding, updating, and deleting documents. It provides examples of loading data from JavaScript and JSON files using the load() and mongoimport commands respectively.

Uploaded by

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

Database contain multiple collections.

Each coolection contain multiple documents.


Each document itself is row when compared with relational database.
Our data will be stored inside separate document and each document is independeant
of others.

Such type of databases are known as document databases.

Database----->Collections----->Documents
Collection----->Table
Document----->Records/Row

meaning of interoperability
2 different type of application can communicate with each other. Java want to
communicatea with Pthon , Python with json,

> db.version()
4.4.5

Sir, from anywhere like from another database can we drop other database??
No you have to be in the database that you hve to drop.

db.getName()
To know in whuch database you are

CRUD
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use durgadb
switched to db durgadb
> db.createCollection('employees')
{ "ok" : 1 }

-----------------------------------------------------------------------------------
--------------------------
db.collection.insertOne()
Here we insert 1 document only

> db.employees.insertOne({eno:100,name:"Sunny",esal:1000,eaddr:"Hyd"})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a51bbb9eb004f74f14017d")
}
> db.employees.insertOne({eno:200,name:"Bunny",esal:2000,eaddr:"Mumbai"})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a51c2a9eb004f74f14017e")
}
> db.employees.insertOne({eno:300,name:"Chinny",esal:3000,eaddr:"Chennai"})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a51c5f9eb004f74f14017f")
}
-----------------------------------------------------------------------------------
-------------------------------
FIND
> db.employees.find({name:"Chinny"})
{ "_id" : ObjectId("60a51c5f9eb004f74f14017f"), "eno" : 300, "name" : "Chinny",
"esal" : 3000, "eaddr" : "Chennai" }
>
If the name is not available then it will return nothing.
If we have writen ({name:"Chinny"}) as ({ename:"Chinny"}) then also error.
> db.employees.find({ename="Vinny"})
uncaught exception: SyntaxError: missing : after property id :
@(shell):1:24
> db.employees.find(ename)
uncaught exception: ReferenceError: ename is not defined :

-------------------
UPDATE
> db.employees.updateOne({ename: "Vinny"},{esal:10000})
uncaught exception: Error: the update operation document must contain atomic
operators :
DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:565:19
@(shell):1:1

>db.employees.updateOne({ename: "Vinny"},{$set: {esal:10000}})

> use studentdb


switched to db studentdb
Till now studentdb is not created.

------------------------
04-Mondodb
db.createCollection("employees",{capped: true, size: 365675})--->valid
If total size of document exceeds 365675 ,automatically can you please delete old
one.

db.createCollection(name)====>Normal collection has no limit of number of document


and no limit of space.
db.createCollection(name,options)
capped
max 1000 documents--->1001 document
size: 3736578 bytes only-->if space completed=====>But i want to specify this much
size only then CAPPED COLLECTION.

If capped is true means that if size exceeds or maximum number of documents


reached, then oldest entry will be deleted automatically.
Default value of capped is FALSE.

>db.createCollection("employees",{capped: true})--->Invalid
"errmsg" : "the 'size' field is required when 'capped' is true",

> db.createCollection("employees",{size: 365675})


{
"ok" : 0,
"errmsg" : "the 'capped' field needs to be true when either the 'size' or
'max' fields are present",
"code" : 72,
"codeName" : "InvalidOptions"
}
>

or SIMPLY
"errmsg" : "the 'capped' field needs to be true when either the 'size' or 'max'
fields are present",

---------------------------
db.employees.insertOne({eno: 100, ename: "Sunny", esal: 1000, eaddr: "Mumbai"})
eno: 100 Here in case of digit we do not put ANY quotes
ename: "Sunny" But here in case of string we put double quotes " ".

Ques-Sir,In each document should have same number of fields like sql?
Ans-NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO

db.collection.insert():
-----------------------
To insert either a single document or multiple documents.
db.employees.insert({...})
db.employees.insert([{..},{..},{..},{..}])

Either we can insert a single document or an array of document

Bulk Insertion
> db.employees.insert([{eno: 800, ename: "Sunny", esal: 1000, eaddr: "Mumbai"},
{eno: 900, ename: "Sunny", esal: 1000, eaddr: "Mumbai"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

Will it make duplicate documents if we add same document multiple times in a


collection?
Only by _id , we can identify that this is duplicate document.

db.employees.insert({_id: "AAA-BBB", name: "Durga"})


db.employees.insertOne({_id: "AAA-BBB", name: "Ravi"})
Here we will get duplicate key error.
Duplicate document are not allowed .
Duplicate document means duplicate _id.

Command are available in javascript file.


Array of document available in json file.
Ordered Insertion
Nested Documents
Array to Document
ObjectID

End of 04-MongoDb

-----------------------------------------------------------------------------------

Createa a document and insert it with any of the method .


Creating Document separately and inserting into collection:
-----------------------------------------------------------
var emp = {};
emp.eno = 7777;
emp.ename = "Bunny";
emp.esal = 777777;
emp.eaddr = "Hyderabad";

db.employees.insertOne(emp)
db.employees.insertMany([emp])
db.employees.insert(emp)
db.employees.insert([emp])

> load("d:/student.js")
true

> use studentdb


switched to db studentdb
> show collections #NO Collections here
> load("d:/student.js")
true
> show collections
students
> db.students.find()
{ "_id" : ObjectId("60aa465158f16af216c31568"), "name" : "Durga", "rollno" : 101,
"marks" : 98 }
{ "_id" : ObjectId("60aa465158f16af216c31569"), "name" : "Ravi", "rollno" : 102,
"marks" : 99 }
{ "_id" : ObjectId("60aa465158f16af216c3156a"), "name" : "Shiva", "rollno" : 103,
"marks" : 100 }
{ "_id" : ObjectId("60aa465158f16af216c3156b"), "name" : "Pavan", "rollno" : 104,
"marks" : 80 }
load()command is applicable for javascript.example:-
> load("d:/student.js")
true

Compulsary this javascript file contain executable commands example:-


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

But now we have data not complete commands


students.json:
--------------
[
{
"name": "Sunny",
"rollno": 666
},
{
"name": "Bunny",
"rollno": 777
},
{
"name": "Chinny",
"rollno": 888
},
{
"name": "Vinny",
"rollno": 999
},
{
"name": "Pinny",
"rollno": 555
}
]

This has no commands we have only data.So now we have to use mongoimport tool
utility.

sir server must be in running naa while we are using mongodbimport?


Server must be in running state.
Because you are inserting into database so mongo server should be running.

end of Video
=====================================================================

You might also like