0% found this document useful (0 votes)
0 views16 pages

20CS3603 Unit-5

The document provides an overview of NoSQL databases, specifically MongoDB, highlighting its features, installation process, and CRUD operations. It contrasts NoSQL with SQL databases, emphasizing MongoDB's flexible schema and document-oriented structure. Additionally, it includes code examples for database operations using MongoDB with Node.js.

Uploaded by

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

20CS3603 Unit-5

The document provides an overview of NoSQL databases, specifically MongoDB, highlighting its features, installation process, and CRUD operations. It contrasts NoSQL with SQL databases, emphasizing MongoDB's flexible schema and document-oriented structure. Additionally, it includes code examples for database operations using MongoDB with Node.js.

Uploaded by

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

MongoDB

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.

Examples of NoSQL databases


MongoDB, Cassandra, Redis, Elasticsearch, BigTable, Neo4j, HBase, and Amazon
DynamoDB.
What is a MongoDB?
MongoDB is an open source, document-oriented database. It is designed to be highly scalable and
offers high developer productivity. MongoDB stores data in JSON-like documents which have
dynamic schema.
NoSQL vs SQL
NoSQL(MongoDB) SQL(Oracle)
Stores data in collections Stores data in tables
Unit of data storage is a document which is stored in a Unit of data storage is a record(table row)
collection
Collections have dynamic schema i.e., documents in Tables have fixed schema i.e., attributes are pre defined
collections have different fields before inserting data. Explicit NULL value has to be
provided if data is missing for an attribute
CRUD operations are performed through insert , find, CRUD operations are performed through
update, and remove operations on collection object INSERT,SELECT,UPDATE and DELETE statements
PRIMARY KEY uniquely identifies a document in a PRIMARY KEY uniquely identifies a record in a table. You
collection. PRIMARY KEY field has a predefined name can choose any name for PRIMARY KEY
_id
NOT NULL, UNIQUE, FOREIGN KEY and CHECK NOT NULL, UNIQUE, FOREIGN KEY and CHECK constraints
constraints are not supported are supported
Joins and Subquery are not supported Joins and Subquery are supported
Installation of MongoDB

Step 1: Download the MongoDB


Community Server
installer from
https://www.mongodb.com/try/downlo
ad/community
mongosh(MongoDB shell)
• The MongoDB Shell, mongosh, is a JavaScript and Node.js REPL(READ EVAL PRINT
LOOP) environment for interacting with MongoDB deployments in Atlas, locally, or on another
remote host.
• MongoDB Shell is used to test queries and interact with the data in your MongoDB database.

Download the mongosh from


https://www.mongodb.com/docs/mongodb-shell/
CRUD operations
The MongoDB shell provides the following methods to insert documents into a collection:
•To insert a single document, use db.collection.insertOne().
•To insert multiple documents, use db.collection.insertMany().

db.collection.insertOne() - Inserts a single document into a collection.


Return

•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({}) SELECT * FROM students;

db.students.find({branch:”cse”}) SELECT * FROM students WHERE branch= “ cse ”

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/'

async function main(){


// create an client instance(or connection object) using MongoClient
Returns the const client= new MongoClient(uri);
promise object try{
// connect the client to database
Specifies to await client.connect();
wait for the console.log('mongodb connected successfully');
event to await getdatabases(client);
happen }catch(e){
console.log(e)
}finally{
client.close()
}
}
main().catch(console.error)
async function getdatabases(client){
const databaseList= await client.db().admin().listDatabases();
console.log("databases")
databaseList.databases.forEach(db=>{
console.log(`-${db.name}`);
})
}

You might also like