20CS3603 Unit-5
20CS3603 Unit-5
Contents
• What is NoSQL database?
• What is the difference between SQL and NoSQL
• What is MongoDB?
• Installation of MongoDB and Mongosh(shell)
• Database operations-CURD(create,Update,Read,Delete)
• MongoDB with Node.js
• Adding mongodb driver to node.js
• Connecting to mongodb using node.js
What
What isisNoSQL?
NoSQL database?
• NoSQL stands for "not only SQL“.
• NoSQL databases are non-relational databases that store data in a flexible schema
model.
• NoSQL databases store data in a non-tabular format
• NoSQL databases are designed to handle large amounts of unstructured data.
• NoSQL databases are well-suited to the large amounts of data generated by the cloud,
mobile, and social media
• NoSQL databases are ideal for developing applications quickly and iteratively.
•A document containing:A
boolean acknowledged as true if the operation ran
with write concern or false if write concern was
disabled.
•A field insertedId with the _id value of the inserted
document.
CRUD operations
Create or insert operations add new documents to a collection. If the collection does not currently
exist, insert operations will create the collection.
MongoDB provides the following methods to insert documents into a collection:
• db.collection.insertOne()
• db.collection.insertMany()
In MongoDB, insert operations target a single collection. All write operations in MongoDB
are atomic on the level of a single document.
collection
db. students. insertOne (
{
name : “William henry”, field : value
branch: “cse”, field : value
gender : “male”, Document
field : value
cgpa : 8.96 field : value
}
)
collection
db. students. insertMany (
[
{
name : “William henry”, field : value
branch: “cse”, field : value
field : value Document
gender : “male”,
cgpa : 8.96 field : value
},
{
name : “jane ”,
branch: “ece”, Document
gender : “female”,
cgpa : 9.16
},
]
)
Read Operation
To display all documents(records) in the collection(table)
MongoDB SQL
db.students.find(
{
branch:”cse”, SELECT * FROM students WHERE branch= “ cse ” and cgpa > 9.00
cgpa:{$gt:9.00}
}
)
db.students.find(
{
branch:”cse”,
cgpa:{$gt:9.00} SELECT rollnumber , name, cgpa FROM students
}, WHERE branch= “ cse ” and cgpa > 9.00
{
rollnumber:1, name: 1(true), cgpa:1
}
)
updateOne(filter, update)
updateMany(filter, update) - returns Promise
The filter used to select the document to update
The update operations to be applied to the document
db.students.updateOne(
{rollno:'20501A1225’}, filter
{$set:{cgpa: 9.31}} update
)
db.students.updateMany(
{
gender:'female’, filter
discount:{$exists:false}
},
{$set:{discount:50}} update
)
const {MongoClient}=require('mongodb');
const uri='mongodb://localhost:27017/'