0
0
MongodbHow-ToBeginner · 2 min read

MongoDB Query to Update Nested Field with Example

Use the $set operator with dot notation to update a nested field, for example: db.collection.updateOne({ _id: 1 }, { $set: { 'nested.field': 'newValue' } }).
📋

Examples

Input{ _id: 1, nested: { field: 'oldValue' } }, update: { $set: { 'nested.field': 'newValue' } }
Output{ _id: 1, nested: { field: 'newValue' } }
Input{ _id: 2, nested: { field: 10, other: 5 } }, update: { $set: { 'nested.field': 20 } }
Output{ _id: 2, nested: { field: 20, other: 5 } }
Input{ _id: 3, nested: { sub: { field: 'a' } } }, update: { $set: { 'nested.sub.field': 'b' } }
Output{ _id: 3, nested: { sub: { field: 'b' } } }
🧠

How to Think About It

To update a nested field in MongoDB, think of the nested field as a path separated by dots. Use the $set operator with this dot notation path to specify exactly which nested field you want to change without affecting other parts of the document.
📐

Algorithm

1
Identify the document to update using a filter.
2
Specify the nested field path using dot notation.
3
Use the $set operator to assign the new value to the nested field.
4
Run the update command to apply the change.
💻

Code

mongodb
db.users.updateOne(
  { _id: 1 },
  { $set: { 'address.city': 'New York' } }
);

printjson(db.users.findOne({ _id: 1 }));
Output
{ "_id" : 1, "name" : "Alice", "address" : { "city" : "New York", "street" : "123 Maple St" } }
🔍

Dry Run

Let's trace updating the city in the nested address field for user with _id 1.

1

Find document

Locate document where _id = 1: { _id: 1, name: 'Alice', address: { city: 'Old City', street: '123 Maple St' } }

2

Apply update

Set 'address.city' to 'New York' using $set operator.

3

Result

Document becomes { _id: 1, name: 'Alice', address: { city: 'New York', street: '123 Maple St' } }

StepActionDocument State
1Find document with _id=1{ _id: 1, address: { city: 'Old City', street: '123 Maple St' } }
2Update 'address.city' to 'New York'{ _id: 1, address: { city: 'New York', street: '123 Maple St' } }
💡

Why This Works

Step 1: Use dot notation

The nested field is accessed by joining keys with dots, like address.city, to target the exact field inside the document.

Step 2: Use $set operator

$set changes only the specified field without overwriting the whole nested object.

Step 3: Update one document

updateOne updates the first matching document, ensuring precise control.

🔄

Alternative Approaches

updateMany
mongodb
db.users.updateMany(
  { 'address.city': 'Old City' },
  { $set: { 'address.city': 'New York' } }
);
Updates all documents matching the filter, useful when multiple documents share the nested field value.
findOneAndUpdate
mongodb
db.users.findOneAndUpdate(
  { _id: 1 },
  { $set: { 'address.city': 'New York' } },
  { returnDocument: 'after' }
);
Returns the updated document immediately, helpful for confirmation or further processing.

Complexity: O(1) time, O(1) space

Time Complexity

Updating a nested field is a direct operation on a single document identified by an index or filter, so it runs in constant time.

Space Complexity

The update modifies the document in place without extra memory allocation beyond the update command.

Which Approach is Fastest?

Using updateOne is fastest for single document updates; updateMany is slower as it updates multiple documents.

ApproachTimeSpaceBest For
updateOne with $set and dot notationO(1)O(1)Single document nested field update
updateMany with $set and dot notationO(n)O(1)Multiple documents nested field update
findOneAndUpdate with $set and dot notationO(1)O(1)Update and return updated document
💡
Always use dot notation with $set to update only the nested field you want without overwriting the entire nested object.
⚠️
Trying to update a nested field without dot notation causes MongoDB to replace the whole nested object instead of just one field.