MongoDB Update One Document


MongoDB Update One Document

In MongoDB, the updateOne operation is used to update a single document within a collection based on a specified filter. This method is essential for modifying specific documents in MongoDB collections.


Syntax

db.collection.updateOne(filter, update, options)

The updateOne method takes a filter to specify which document to update, an update document to specify the modifications, and an optional options parameter to customize the operation.


Example MongoDB Update One Document

Let's look at some examples of how to use the updateOne method in the programGuru collection in MongoDB:

1. Update a Single Document

db.programGuru.updateOne(
    { name: "John Doe" },
    { $set: { age: 31 } }
)

This command finds a document where the name is John Doe and updates the age to 31.

2. Update a Document with Options

db.programGuru.updateOne(
    { name: "Jane Smith" },
    { $set: { age: 26 } },
    { upsert: true }
)

This command finds and updates a document where the name is Jane Smith, and if no such document exists, it inserts a new document with the specified fields.


Full Example

Let's go through a complete example that includes switching to a database, creating a collection, inserting documents, and updating a document.

Step 1: Switch to a Database

This step involves switching to a database named myDatabase.

use myDatabase

In this example, we switch to the myDatabase database.

MongoDB Update One Document

Step 2: Create a Collection

This step involves creating a new collection named programGuru in the myDatabase database.

db.createCollection("programGuru")

Here, we create a collection named programGuru.

MongoDB Update One Document

Step 3: Insert Documents into the Collection

This step involves inserting documents into the programGuru collection.

db.programGuru.insertMany([
    { name: "John Doe", age: 30, email: "john.doe@example.com" },
    { name: "Jane Smith", age: 25, email: "jane.smith@example.com" },
    { name: "Jim Brown", age: 35, email: "jim.brown@example.com" }
])

We insert multiple documents into the programGuru collection.

MongoDB Update One Document

Step 4: Update a Document in the Collection

This step involves using the updateOne method to update a document in the programGuru collection.

Update a Single Document

db.programGuru.updateOne(
    { name: "John Doe" },
    { $set: { age: 31 } }
)

We find a document where the name is John Doe and update the age to 31.

Update a Document with Options

db.programGuru.updateOne(
    { name: "Jane Smith" },
    { $set: { age: 26 } },
    { upsert: true }
)

We find and update a document where the name is Jane Smith, and if no such document exists, we insert a new document with the specified fields.

MongoDB Update One Document

Conclusion

The MongoDB updateOne operation is crucial for modifying specific documents in collections. Understanding how to use this method allows you to efficiently manage and update data within MongoDB collections.