To update a document (or Multiple Documents) in MongoDB.
we can use the "updateOne()" method to update a single document or the "updateMany()" method to update multiple documents that match specific criteria.
Alternatively, we can also use the "findOneAndUpdate()" method to find a single document and update it atomically.
Here's how we can update documents in MongoDB using the MongoDB shell:
Before creating a Collection(table) in MongoDB, First, we need to connect to our MongoDB instance using the MongoDB shell or a MongoDB client library using our programming language.
Using the Mongo shell, we can connect to a MongoDB server running on localhost by simply typing the `mongo` command in our terminal.
mongo
Once our connection is established successfully, we can check databases in MongoDB using the below command:
show dbs;
Switch to the database where you want to create the collection using the "use" command.
use your_database_name;
To check the current database name using mongo shell.
db
Use the "createCollection()" method to explicitly create a collection.
db.createCollection("your_collection_name")
# Connect to MongoDB (assuming it's running locally on the default port) mongo # Switch to the desired database use your_database_name # Create a collection named your_collection_name db.createCollection("your_collection_name")
The above command will create a collection in our specified Database.
# Insert multiple documents into a collection db.your_collection_name.insertMany([ {name: "Alice", Department: "HR", age: 36, city: "London"}, {name: "Bella", Department: "Director", age: 32, city: "Polo Alto"}, {name: "Warner", Department: "Business Analyst", age: 30, city: "London"} ])
Use the appropriate update method to modify the document(s) in the collection.
# Connect to MongoDB (assuming it's running locally on the default port) mongo # Switch to the desired database use your_database_name # Update a single document in the collection db.your_collection_name.updateOne( { name: "Alice", department: "HR" }, // Filter criteria { $set: { age: 30 } } // Update operation ) # Update multiple documents in the collection db.your_collection_name.updateMany( { city: "London" }, // Filter criteria { $set: { city: "Boston" } } // Update operation )
Replace "your_database_name" with the name of your database and "your_collection_name" with the name of your collection.
Adjust the filter criteria and update operations as per your requirements.
"$set" is an update operator that sets the value of a field in the document.
MongoDB provides various other update operators like " $inc, $unset, $push, $addToSet, etc. ", to perform different updates.
we can update a single document and return the original document or the updated document, for that we can use the "findOneAndUpdate()" method In MongoDB.
# Find a document and update it, returning the original document db.your_collection_name.findOneAndUpdate( { name: "Alice Collin" }, // Filter criteria { $set: { age: 25 } }, // Update operation { returnOriginal: false } // Option to return the updated document )
Above Query, will update the document attribute `age` value to "25" and return either the original or updated document, depending on the options provided.