Group B Assign 1 Crud Operat
Group B Assign 1 Crud Operat
Group B Assign 1 Crud Operat
RDBMS concepts
RDBMS MongoDB
Database Database
Table, View Collection
2
Row Document (JSON, BSON)
Column Field
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard
JSON
“JavaScript Object Notation”
Built on
• name/value pairs
• Ordered list of values
http://json.org/
BSON
“Binary JSON”
Goals
• Lightweight
• Traversable
• Efficient (decoding and encoding)
http://bsonspec.org/
BSON Example
{
"_id" : "37010"
“City" : “Nashik",
“Pin" : 423201,
"state" : “MH",
“Postman” : {
name: “Ramesh Jadhav”
address: “Panchavati”
}
}
Data Types of MongoDB
Integer
Date Boolean
Object ID String
Null Arrays
Data Types
• String : This is most commonly used datatype to store the data. String
in mongodb must be UTF-8 valid.
• Integer : This type is used to store a numerical value. Integer can be
32 bit or 64 bit depending upon your server.
• Boolean : This type is used to store a boolean (true/ false) value.
• Double : This type is used to store floating point values.
• Min/ Max keys : This type is used to compare a value against the
lowest and highest BSON elements.
• Arrays : This type is used to store arrays or list or multiple values into
one key.
• Timestamp : ctimestamp. This can be handy for recording when a
document has been modified or added.
• Object : This datatype is used for embedded documents.
Data Types
• Null : This type is used to store a Null value.
• Symbol : This datatype is used identically to a string however,
it's generally reserved for languages that use a specific symbol
type.
• Date : This datatype is used to store the current date or time in
UNIX time format. You can specify your own date time by
creating object of Date and passing day, month, year into it.
• Object ID : This datatype is used to store the document’s ID.
• Binary data : This datatype is used to store binay data.
• Code : This datatype is used to store javascript code into
document.
• Regular expression : This datatype is used to store regular
expression
Basic Database Operations
Database
collection
Basic Database Operations- Database
use <database • switched to database provided with
name> ciommand
db.dropDatabase
• To Drop the database
()
Basic Database Operations- Collection
db.createCollection (name)
• To create collection
Ex:- db.createCollection(Stud)
Find
Update
Delete
CRUD Operations - Insert
• The insert() Method:- To insert data into MongoDB collection,
you need to use MongoDB's insert() or save()method.
• Syntax
>db.COLLECTION_NAME.insert(document)
• Example
>db.stud.insert({name: “Jiya”, age:15})
CRUD Operations - Insert
• _id Field
• If the document does not specify an _id field, then MongoDB
will add the _id field and assign a unique ObjectId for the
document before inserting.
3 Bytes- Machine Id
• db.stud.find()
{ "_id" : "5063114bd386d8fadbd6b004”, “Name” : “Reena", “Rno”: 15 }
• db.stud.find()
{ "_id" : 10, “Name” : “Reena", “Rno”: 15 }
CRUD Operations - Insert
• Insert Single Documents
db.stud.insert
( {Name: “Ankit”, Rno:1, Address: “Pune”} )
CRUD Operations - Insert
• Insert Multiple Documents
db.stud.insert
([
{ Name: “Ankit”, Rno:1, Address: “Pune”} ,
{ Name: “Sagar”, Rno:2},
{ Name: “Neha”, Rno:3}
])
CRUD Operations - Insert
• Insert Multicolumn attribute
db.stud.insert(
{
Name: “Ritu",
Address: { City: “Pune",
State: “MH” },
Rno: 6
}
)
CRUD Operations - Insert
• Insert Multivalued attribute
db.stud.insert(
{
Name : “Sneha",
Hobbies: [“Singing”, “Dancing” , “Cricket”] ,
Rno:8
}
)
CRUD Operations - Insert
• Insert Multivalued with Multicolumn attribute
db.stud.insert(
{
Name : “Sneha",
Awards: [ { Award : “Dancing”, Rank: “1 st”, Year: 2008 },
{Award : “Drawing”, Rank: “3rd”, Year: 2010 } ,
{Award : “Singing”, Rank: “1st”, Year: 2015 } ],
Rno: 9
}
)
CRUD Operations - Insert
db.source.copyTo(target)
Find
Update
Delete
CRUD Operations - Find
• The find() Method- To display data from MongoDB collection.
Displays all the documents in a non structured way.
• Syntax
>db.COLLECTION_NAME.find()
• Syntax
>db. COLLECTION_NAME.find().pretty()
CRUD Operations - Find
$lte Matches values that are less than or equal to a specified value.
$ne Matches all values that are not equal to a specified value.
db.stud.find({name: “Jiya”},{Rno:1})
To show the rollno of student whose name is equal to
Jiya (by default _id is also shown)
db.stud.find({name: “jiya”},{_id:0,Rno:1})
show the rollno of student whose name is equal to
Jiya (_id is not shown)
CRUD Operations – Find
Examples for Sort function
db.stud.find().sort( { Rno: 1 } )
Sort on age field in Ascending order (1)
db.stud.find().sort( { Rno: -1 } )
Sort on age field in Ascending order(-1)
CRUD Operations – Find Examples
of Count functions
db.stud.find().count()
Returns no of documents in the collection
db.stud.find({Rno:2}).count()
Returns no of documents in the collection
which satisfies the given condition Rno=2
CRUD Operations – Find Examples
of limit and skip
db.stud.find().limit(2)
Returns only first 2 documents
db.stud.find().skip(5)
Returns all documents except first 5
documents
CRUD Operations – Find Examples
of limit and skip
db.stud.find({ rno: { $gt:5} } ).limit(2)
Returns only first 2 documents whose rno
is greater than 5
db.stud.find({“Address.city”: “Pune”})-
Finding in Multicolumned attribute
db.stud.find({name: “Riya”,age:20})
Find documents whose name is Riya and Rno is 20
CRUD Operations – Find Examples
with in and not in operator
db.stud.find({name:{$in:[“riya”,”jiya”]}})
Find information whose name is riya or jiya
db.stud.find({Rno:{$nin:[20,25]}})
Find information whose rollno is not 20 or 25
CRUD Operations – Find Examples
for Distinct clause
db.stud.distinct(“Address”)
Find from which different cities students
are coming
CRUD Operations – Find Examples
similar to like operator
db.stud.find({name:/^n/})
Find students whose name starts with n
db.stud.find({name:/n/})
Find students whose name contains n letter
db.stud.find({name:/n$/})
Find students whose name ends with n
CRUD Operations – Find Examples
db.collection.stats()
db.collection.explain().find()
db.collection.explain().find().help()
CRUD Operations
Insert
Find
Update
Delete
CRUD Operations – Update
•Syntax
db.CollectionName.update(
<query/Condition>,
<update with $set or $unset>,
{
upsert: <boolean>,
multi: <boolean>,
}
)
CRUD Operations – Update
db.stud.update(
• Set age = 25 where id is 100
{ _id: 100 }, • Only the age field of one document is updated
where condition is matched
{ $set:{age: 25}})
db.stud.update(
• To remove a age column from single document
{ _id: 100 }, where id=100
{ $unset:{age: 1}})
CRUD Operations – Update
Examples
db.stud.update( • Set marks for dbms subject as 50
{ _id: 100 }, where id = 100 (only one row is
{ $set: { “marks.dmsa": 50} }) updated)
db.stud.update(
{ class: “TE” }, • Set marks for dbms subject as 50
where class is TE (all rows which
{ $set: { “marks.dmsa": 50} } , matches the condition were updated)
{ multi: true } )
db.stud.update( • Set marks for dbms subject as 50 where
{ class: “TE” }, class is TE (all rows which matches the
condition were updated)
{ $set: { “marks.dmsa": 50} } , • If now row found which matches the
{ upsert: true } ) condition it will insert new row.
CRUD Operations – Update
Examples
db.stud.update
({ },{ $inc:{age: 5}})
db.stud.update
({ },{ $set:{cadd: “Pune”}},
{multi:true})
db.stud.update
({ },{ $rename:{“age”:
“Age”}},{multi:true})
CRUD Operations
Insert
Find
Update
Delete
CRUD Operations – Remove
Remove All
• db.inventory.remove({})
Documents
Remove All
• db.inventory.remove
Documents that ( { type : "food" } )
Match a Condition
Remove a Single
• db.inventory.remove
Document that ( { type : "food" }, 1 )
Matches a Condition
References
• https://docs.mongodb.com/manual/introduction/
• http://metadata-standards.org/Document-library/Documents-
by-number/WG2-N1501-
N1550/WG2_N1537_SQL_Standard_and_NoSQL_Databases%
202011-05.ppt
• https://www.slideshare.net/raviteja2007/introduction-to-
mongodb-12246792
• https://docs.mongodb.com/manual/core/databases-and-
collections/
• https://docs.mongodb.com/manual/crud/