DEV Community 👩‍💻👨‍💻

Asif Mohammed Sifat
Asif Mohammed Sifat

Posted on

MongoDB CRUD Operations

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()

  1. db.collection.insertMany() InsetOne Method:
await db.collection('inventory').insertOne({
  item: 'canvas',
  qty: 100,
  tags: ['cotton'],
  size: { h: 28, w: 35.5, uom: 'cm' }
});
Enter fullscreen mode Exit fullscreen mode

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'] }
});
Enter fullscreen mode Exit fullscreen mode

Update Operations:
Update operations modify the existing documents in a collection. MongoDB provides the following methods.

  1. db.collection.updateOne()
  2. db.collection.updateMany()
  3. 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
   }
)

Enter fullscreen mode Exit fullscreen mode

UpdateMany Method:

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        
   }
)
Enter fullscreen mode Exit fullscreen mode

Replace One Method:

db.collection.replaceOne(
   <filter>,
   <replacement>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     hint: <document|string>                   
   }
)
Enter fullscreen mode Exit fullscreen mode

Delete Operations:
Delete operations remove documents from a collection.

  1. db.collection.deleteOne()
  2. db.collection.deleteMany()

DeleteOne Method:

db.collection.deleteOne(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>,
      hint: <document|string>        // Available starting in MongoDB 4.4
   }
)
Enter fullscreen mode Exit fullscreen mode

Delete Many Method

db.collection.deleteMany(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>
   }
)


Enter fullscreen mode Exit fullscreen mode

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.

Image description
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}`)
})

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.