Perform CRUD in Mongodb with Expressjs

In this article will discuss about CRUD operations in MongoDB.

CRUD Operations it is CREATE,READ,UPDATE,DELETE these are the operations that user will use to do ceratin operations in mongodb server.

Basically,MongoDB is NoSQL database used to store very huge amount of data without using table like in SQL  instead it uses rows and columns.

We firstly download mongodb Community server from https://www.mongodb.com/try/download/community

Secondly we must download certain package which are listed below:

npm install express
npm install mongoose

 

1. Create Operations

To add new documents to a collection , use the CREATE operation.

const express=require('express')
const mongoose = require('mongoose')
const mongourl='mongodb://localhost:27017/employee'

const connectTOMongo=()=>{
    mongoose.connect(mongourl)
    console.log('Connected to mongodb Successfully');
}
connectTOMongo()
const userSchema=new mongoose.Schema({
    name:String,
    salary:Number
})

const User=mongoose.model('User',userSchema)

Output:

PS C:\Users\HP\Desktop\CODESPEEDY\CRUD IN MONGODB> node app.js
Connected to mongodb Successfully

 

From the above mentioned code we have firstly connect to the mongodb and we also
make the schema and model.

 Insert Operations

For insert operations we have 2 types of query:

  1. db.collection.insertOne()
  2. db.collection.insertMany()
employee> db.User.insertOne({name:"adam",salary:80000})
{
  acknowledged: true,
  insertedId: ObjectId('6662c408be59bd7640cdcdf6')
}
employee> db.User.find()
[
  {
    _id: ObjectId('6662c408be59bd7640cdcdf6'),
    name: 'adam',
    salary: 80000
  }
]
employee> db.User.insertMany([{name:"bob",salary:70000},{name:"alice",salary:50000}])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('6662c45ebe59bd7640cdcdf7'),
    '1': ObjectId('6662c45ebe59bd7640cdcdf8')
  }
}

2.Read Operations

Data from a collection is retrieved using READ Operation .It is used to query a collection for a document.

It have following type of query:

db.collection.find()
employee> db.User.find()
[
  {
    _id: ObjectId('6662c408be59bd7640cdcdf6'),
    name: 'adam',
    salary: 80000
  },
  {
    _id: ObjectId('6662c45ebe59bd7640cdcdf7'),
    name: 'bob',
    salary: 70000
  },
  {
    _id: ObjectId('6662c45ebe59bd7640cdcdf8'),
    name: 'alice',
    salary: 50000
  }
]

 

3.Update Operations

The Update operation is used to edit the existing document in collection.

It having following types of query:

  1. db.collection.updateOne()
  2. db.collection.updateMany()
employee> db.User.updateOne({name:"alice"},{$set:{salary:8000}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
employee> db.User.find()
[
  {
    _id: ObjectId('6662c408be59bd7640cdcdf6'),
    name: 'adam',
    salary: 80000
  },
  {
    _id: ObjectId('6662c45ebe59bd7640cdcdf7'),
    name: 'bob',
    salary: 70000
  },
  {
    _id: ObjectId('6662c45ebe59bd7640cdcdf8'),
    name: 'alice',
    salary: 8000
  }
]

4.Delete Operations

Collection of document can be deleted  using DELETE operation.

It having following types of query:

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

 

employee> db.User.deleteOne({name:"bob"})
{ acknowledged: true, deletedCount: 1 }
employee> db.User.find()
[
  {
    _id: ObjectId('6662c408be59bd7640cdcdf6'),
    name: 'adam',
    salary: 80000
  },
  {
    _id: ObjectId('6662c45ebe59bd7640cdcdf8'),
    name: 'alice',
    salary: 8000
  }
]

 

Leave a Reply

Your email address will not be published. Required fields are marked *