Update Document MongoDB

Update Document MongoDB

Instructor-svgAl-Mamun Sarkar
Sep 11 , 2020

In this lesson, I will show you how to update a document on the MongoDB database. 

Update Query:

Syntax:

db.collection.update(select_condition, data_for_updae)

 

Update Name WHERE by ID:

db.employees.update({ "_id": ObjectId("5f5b1a7717b9204b9813c95f") }, { $set: { "name": "New Name" } })

The following queres only update one row: 

db.employees.update({ "salary": {$lt: 500} }, { $set: { "phone": "0172345466465" } })
db.employees.findOneAndUpdate( { "salary": {$lt: 500} }, { $set: { "phone": "012923493245" } }, {multi:true} )

 

Update Multiple row:

db.employees.update( { "salary": {$lt: 500} }, { $set: { "phone": "01888823854" } }, {multi:true} )

 

Replace Document by New document:

Syntax:

db.collection.save({_id:ObjectId(),new_data})

Example:

db.employees.save( {_id: ObjectId("5f5b1a7717b9204b9813c95f"), "name" : "Tamim Iqbal", "email" : "tamim@gmail.com", "salary" : 500 } )

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram