In this tutorial we will learn about how to update values in a document and how to delete documents in monodb.
Retrieve the document from the database
Note: We are using testDB database, and from the previous tutorial we are using collection as testCollection
1) Open a new command Prompt
2) Enter “mongod
“, and press ENTER (it will start your mongodb server. Don’t close it)
3) Open Robo3T.
4) Right click on Connection name.
5) Open shell.
6)Write “use testDB
“, and press CTRL+ENTER. It will switch to testDB database.
7) Write “db.testCollection.find({})
” , and press CTRL+ENTER.
You find
First document as Name : Car, Value: A
Second document as Name: BIKE, Value: B
Third document as Name : MOTORCYCLE, Value: C
Update Documents in MongoDB
MongoDB Command to Update a Document : The command is “db.collectionName.update({selectionCriteria}, {$set: {updateValue}})
”.
It finds the documents which satisfy the selectionCriteria, and then according to the updateValue it updates the document.
Example: Change the document Name to “Vehicle Not Running” which has Value as A.
selectionCriteria = Value:A, updateValue: Name: Vehicle Not Running
Mongodb command:
“db.testCollection.update({“Value”:”A”}, {$set:{“Name”:” Vehicle Not Running”}})"
Write Mongodb command in the shell and press CTRL+ENTER. Output will be OK.
Now write “db.testCollection.find({})
”, in the sheel and press CTRL+ENTER.
You find
First document as Name : Vehicle Not Running, Value: A
Second document as Name: BIKE, Value: B
Third document as Name : MOTORCYCLE, Value: C
Note: If there were two documents which satisfy the selectionCriteria, it will update only the document with which it matches first. There are two way to doing this.
1) Use updateMany instead of update.
MongoDB Command to Update all Document which satisfy the selectionCriteria: The command is
“db.collectionName.updateMany({selectionCriteria}, {$set: {updateValue}})
”.
It finds all the documents which satisfy the selectionCriteria, and then according to the updateValue it updates all documents.
2) Use {multi:true} after the update value.
MongoDB Command to Update all Document with multi: The command is
“db.collectionName.update({selectionCriteria}, {$set: {updateValue}}, {multi:true})
”.
Delete Documents from a Collection
MongoDB command to delete documents from a collection: The command is
“db.collectionName.remove({selectionCriteria})
”
It finds all the documents which satisfy the selectionCriteria, and then deletes them.
Note: If you want to delete only one document which satisfy the selectionCriteria you have to write 1 after the selectionCriteria in the mongodb command.
Mongodb command to delete only one document is:
“db.collectionName.remove({selectionCriteria}, 1)
”
Example: Let us say you have selection criteria as Value A. When you write the find query to find all the documents which have value as A. You find:
- Name: CAR, Value:A
- Name:Bike, Value:A
If you write “db.testCollection.remove({“Value”:”A”})
” in the shell and press CTRL+ENTER, it will delete both the documents.
But If you write “db.testCollection.remove({“Value”:”A”}, 1)
” in the shell and press CTRL+ENTER, it will delete only the document which first matches with the selection criteria.
Selecting Fields
MongoDb command to retrieve only a field (or more than one field) of all the documents from a collection
“db.collectionName({selectionCriteria},{“fieldName”:x})
”
x would be 0 or 1, depending upon the condition. If you want to see the field with name as fieldName then x is 1, otherwise 0. Default is 0.
For example: You have four documents in testCollection:
- FirstName: Shubh, LastName: Dubey, Salary:40000, Location: Delhi
- FirstName: Apoorv, LastName: Katiyar, Salary:50000, Location: Mumbai
- FirstName: Ayush, LastName: Singh, Salary:20000, Location: Chennai
- FirstName: Alok, LastName: Maurya, Salary:80000, Location: Kolkata
A. Retrieve the FirstName and Location of all the documents.
Mongodb command is: “db.testCollection.find({},{“FirstName”:1, ”Location”:1})
”
Write Mongodb command in the shell and press CTRL+ENTER.
It will display:
- FirstName: Shubh, Location: Delhi
- FirstName: Apoorv, Location: Mumbai
- FirstName: Ayush, Location: Chennai
- FirstName: Alok, Location: Kolkata
B. Retrieve the FirstName , LastName and Location of the documents which have their salary 80000.
Mongodb command is: “db.testCollection.find({“salary”:80000},{“FirstName”:1,”LastName”:1, “Location”:1})
”
Write Mongodb command in the shell and press CTRL+ENTER.
It will display:
FirstName: Alok, LastName: Maurya, Location: Kolkata
If you have any queries regarding update and delete documents in MongoDB, drop a comment below.