MongoDB CRUD Operation:
CRUD stands for
C- create,
R- read,
U-update,
D- delete
Create Operations:
MongoDB has two insert method:
1.db.collection.insertOne()
- db.collection.insertMany() InsetOne Method:
await db.collection('inventory').insertOne({
item: 'canvas',
qty: 100,
tags: ['cotton'],
size: { h: 28, w: 35.5, uom: 'cm' }
});
InsetMany Method:
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
tags: ['blank', 'red'],
size: { h: 14, w: 21, uom: 'cm' }
},
{
item: 'mousepad',
qty: 25,
tags: ['gel', 'blue'],
size: { h: 19, w: 22.85, uom: 'cm' }
}
]);
Read Operations:
Read operations retrieve documents from a collection.
db.collection.find()
Find METHOD:
const cursor = db.collection('inventory').find({
status: { $in: ['A', 'D'] }
});
Update Operations:
Update operations modify the existing documents in a collection. MongoDB provides the following methods.
- db.collection.updateOne()
- db.collection.updateMany()
- db.collection.replaceOne() UpdateOne Method:
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2.1
}
)
UpdateMany Method:
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
}
)
Replace One Method:
db.collection.replaceOne(
<filter>,
<replacement>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
hint: <document|string>
}
)
Delete Operations:
Delete operations remove documents from a collection.
- db.collection.deleteOne()
- db.collection.deleteMany()
DeleteOne Method:
db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>,
hint: <document|string> // Available starting in MongoDB 4.4
}
)
Delete Many Method
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
Relational Database
What is a Relational Database?
A relational database defines database relationships in the form of tables. The tables are related to each other - based on data common to each.
In Above Fig, Structure of the relational database Each box on the figure above contains one table called here the relation of the database.
**
Express.js Hello World Example:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Top comments (0)