Mongo DB Notes by Tishant Agrawal
Mongo DB Notes by Tishant Agrawal
It provides a mechanism for storage and retrieval of data other than tabular
relations model used in relational databases. NoSQL database doesn't use
tables for storing data. It is generally used to store big data and real-time web
applications.
operations.
between data.
gaming.
mongoDB
● Data layer
Application Layer is also known as the Final Abstraction Layer, it has
two-parts, first is a Frontend (User Interface) and the second is
Backend (server). The frontend is the place where the user uses
MongoDB with the help of a Web or Mobile. This web and mobile
include web pages, mobile applications, android default applications,
IOS applications, etc. The backend contains a server which is used to
perform server-side logic and also contain drivers or mongo shell to
interact with MongoDB server with the help of queries.
These queries are sent to the MongoDB server present in the Data
Layer. Now, the MongoDB server receives the queries and passes the
received queries to the storage engine. MongoDB server itself does not
directly read or write the data to the files or disk or memory. After
passing the received queries to the storage engine, the storage engine
is responsible to read or write the data in the files or memory basically
it manages the data.
MongoDB RDBMS
It is a non-relational and
It is a relational database.
document-oriented database.
Advantages of MongoDB :
MongoDB.
BSON documents.
Disadvantages of MongoDB :
● You are not allowed to store more than 16MB data in the
documents.
your database.
to tables in RDBMS.
JSON
The data types supported by JSON are string, number, object, array, true,
false, and null.
For example
{
"_id" : 100,
"name" : "John",
"subject" : [
"Maths",
"English",
"Hindi"
],
"address" : {
"city" : "faridabad",
"state" : "haryana"
}
BSON
It supports the adding of documents and arrays within other documents and
arrays.
Lightweight: When used over the network, the JSON keeps the
overhead involved in processing extra header data to a minimum.
○ Efficient: It allows C data types that allow easy and quick encoding
and decoding of data.
JSON BSON
null.
Databases like AnyDB, redis, etc. The data in MongoDB is stored in a BSON
format.
BSON.
data.
It does not have encoding and It has faster encoding and decoding
information from the JSON file then skips all the content which are not in use.
content.
JSON format does not require to be It requires to be parsed as it can be easily
readable.
collection of key-value pairs while length of the string and object subtypes.
array is an ordered list of elements. BinData and Date are the additional data
JSON vs BSON
JSON BSON
What is sharding?
Sharding is a type of database partitioning that separates large
databases into smaller, faster, more easily managed parts. These
smaller parts are called data shards. The word shard means "a
small part of a whole."
c-programs files-mongoDB-server-5.0-bin
show database
show dbs (all database show here)
create Collection
db.createCollection('proud')
db thn enter
current database show
use product
switched to db product
> db product
> db.createCollection('prod')
{ "ok" : 1 }
show dbs
admin 0.000GB
config 0.000GB
emp 0.000GB
employe 0.000GB
inventory 0.000GB
local 0.000GB
product 0.000GB
test 0.000GB
show collections
prod
show collections
prod
users
show collections
prod
(only one collection here)
db.dropDatabase()
(for delete database drop)
db.createCollection('books')
(create a new collection)
show collections
Read operation
insert 3 types
insert()
insertOne()
insertMany()
insert ():
db.books.find()
to check insert data in collections
insertOne()
db.books.insertOne({name:'PHP' , price:'1000' , pages:'580'})
{
"acknowledged" : true,
"insertedId" : ObjectId("64fffff1755d25d0ba46fac4")
}
insertMany()
db.books.insertMany([{}.{},{},{}])
multiple array separate by ,
for ex
db.books.insertMany([{name:'PHP',price:'1000',pages:'580'},{name:'j
avascript',pages:'200'}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("65000148755d25d0ba46fac5"),
ObjectId("65000148755d25d0ba46fac6")
]
db.books.find()
all inserted data are here
Update operation
update 3 types
update ()
update One()
updateMany()
update ()
db.books.update({_id:
ObjectId("64ffff11755d25d0ba46fac3")},{name:'reference
java',pages:'6000', price:'900'})
db.books.find()
{ "_id" : ObjectId("64ffff11755d25d0ba46fac3"), "name" : "reference
java", "pages" : "6000", "price" : "900" }
{ "_id" : ObjectId("64fffff1755d25d0ba46fac4"), "name" : "PHP",
"price" : "1000", "pages" : "580" }
{ "_id" : ObjectId("65000148755d25d0ba46fac5"), "name" : "PHP",
"price" : "1000", "pages" : "580" }
{ "_id" : ObjectId("65000148755d25d0ba46fac6"), "name" :
"javascript", "pages" : "200" }
{ "_id" : ObjectId("65000204755d25d0ba46fac7"), "name" : "PHP",
"price" : "1000", "pages" : "580", "author" : "harry" }
updateOne()
db.books.updateOne({_id:
ObjectId("64ffff11755d25d0ba46fac3")},{$set:{name:'playing
book',price:'200',pages:'700'}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
db.books.find()
db.books.updateOne({_id:
ObjectId("64fffff1755d25d0ba46fac4")},{$rename:{name:'bookname'
}})
db.books.find()
{ "_id" : ObjectId("64ffff11755d25d0ba46fac3"), "name" : "playing
book", "pages" : "700", "price" : "200" }
{ "_id" : ObjectId("64fffff1755d25d0ba46fac4"), "price" : "1000",
"pages" : "580", "bookname" : "PHP" }
{ "_id" : ObjectId("65000148755d25d0ba46fac5"), "name" : "PHP",
"price" : "1000", "pages" : "580" }
{ "_id" : ObjectId("65000148755d25d0ba46fac6"), "name" :
"javascript", "pages" : "200" }
{ "_id" : ObjectId("65000204755d25d0ba46fac7"), "name" : "PHP",
"price" : "1000", "pages" : "580", "author" : "harry" }
remove()
deleteOne()
deleteMany()
remove()
db.books.remove({_id: ObjectId("64ffff11755d25d0ba46fac3")})
db.books.find()
db.books.find().pretty()
{
"_id" : ObjectId("64fffff1755d25d0ba46fac4"),
"price" : "1000",
"pages" : "580",
"bookname" : "PHP"
}
{
"_id" : ObjectId("65000148755d25d0ba46fac5"),
"name" : "PHP",
"price" : "1000",
"pages" : "580"
}
{
"_id" : ObjectId("65000148755d25d0ba46fac6"),
"pages" : "200",
"playing book" : "javascript"
}
{
"_id" : ObjectId("65000204755d25d0ba46fac7"),
"name" : "PHP",
"price" : "1000",
"pages" : "580",
"author" : "harry"
}
limit()
db.books.find().limit(2)
{ "_id" : ObjectId("64fffff1755d25d0ba46fac4"), "price" : "1000",
"pages" : "580", "bookname" : "PHP" }
{ "_id" : ObjectId("65000148755d25d0ba46fac5"), "name" : "PHP",
"price" : "1000", "pages" : "580" }
limit().pretty()
db.books.find().limit(2).pretty()
{
"_id" : ObjectId("64fffff1755d25d0ba46fac4"),
"price" : "1000",
"pages" : "580",
"bookname" : "PHP"
}
{
"_id" : ObjectId("65000148755d25d0ba46fac5"),
"name" : "PHP",
"price" : "1000",
"pages" : "580"
}
sort()
db.books.find().sort({price:1})
{ "_id" : ObjectId("65000148755d25d0ba46fac6"), "pages" : "200",
"playing book" : "javascript" }
{ "_id" : ObjectId("64fffff1755d25d0ba46fac4"), "price" : "1000",
"pages" : "580", "bookname" : "PHP" }
{ "_id" : ObjectId("65000148755d25d0ba46fac5"), "name" : "PHP",
"price" : "1000", "pages" : "580" }
{ "_id" : ObjectId("65000204755d25d0ba46fac7"), "name" : "PHP",
"price" : "1000", "pages" : "580", "author" : "harry" }
db.books.find().sort({price:1}).pretty()
db.books.find().sort({price:-1})(desencding order)