Update Document

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 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:

Connect to MongoDB:

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.

Example:

mongo

Example:

show dbs;

Once our connection is established successfully, we can check databases in MongoDB using the below command:

Example:

use your_database_name;

Example:

db

Switch or Create Database:

Switch to the database where you want to create the collection using the "use" command.

To check the current database name using mongo shell.

Example:

db.createCollection("your_collection_name")

Create Collection:

Use the "createCollection()" method to explicitly create a collection.

Example:

# 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.

Note: Replace "your_database_name" with the name of your database and "your_collection_name" with the name you want for your collection.

Insert Documents in Collections:

# 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"}
])

Update Document(s):

Use the appropriate update method to modify the document(s) in the collection.

Update Document Example:

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

Example:

# 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

)

Note:

  • 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.

Example -> findOneAndUpdate Method

we can update a single document and return the original document or the updated document, for that we can use the "findOneAndUpdate()" method:

Above Query, will update the document with the age to "25" and return either the original or updated document, depending on the options provided.