---
title: "Write a MongoDB query to update specific fields in multiple documents in a collection."  
description: "Write a MongoDB query to update specific fields in multiple documents in a collection."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159307/write-a-mongodb-query-to-update-specific-fields-in-multiple-documents-in-a-collection  
category: "mongodb"  
tags: ["database", "mongodb"]  
reading_time: 2 minutes  

---

# Write a MongoDB query to update specific fields in multiple documents in a collection.

Write a [MongoDB query](https://www.mindstick.com/forum/159295/write-a-mongodb-query-to-find-all-documents-in-a-collection-with-a-specific-condition) to [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) specific [fields](https://www.mindstick.com/forum/159126/sql-query-for-all-tables-and-fields-in-an-oracle-db) in [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [documents](https://www.mindstick.com/articles/156931/the-main-types-of-business-documents) in a collection.

## Replies

### Reply by Aryan Kumar

Sure, here is a [MongoDB](https://www.mindstick.com/articles/337565/understanding-document-stores-a-look-at-mongodb-vs-couchdb) [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to update specific fields in multiple documents in a collection:

```plaintext
db.collection.updateMany({ name: 'John Doe' }, { $set: { age: 35, address: '123 Main Street' } });
```

This query updates the `age` and `address` fields in all documents in the collection where the `name` field is equal to `John Doe`. The `$set` operator is used to update the fields, and the values of the fields are specified in the object literal.

For example, if the collection contains the following documents:

```plaintext
{ name: 'John Doe', age: 30, address: '456 Elm Street' }
{ name: 'Jane Doe', age: 25, address: '789 Oak Street' }
```

Then, the query would update the `age` and `address` fields in the first document to 35 and `123 Main Street`, respectively. The second document would not be updated, because the `name` field does not match the filter.

To run this query, you would need to connect to the MongoDB database and get the collection. Then, you could execute the query using the `updateMany()` method.

Here is an example of how you could run the query:

```plaintext
const client = new MongoClient('mongodb://localhost:27017/mydb');
const db = client.db('mydb');
const collection = db.collection('people');

collection.updateMany({ name: 'John Doe' }, { $set: { age: 35, address: '123 Main Street' } });
```

This code would connect to the MongoDB database and get the `people` collection. Then, it would execute the query to update the `age` and `address` fields in the document where the `name` field is equal to `John Doe`.


---

Original Source: https://www.mindstick.com/forum/159307/write-a-mongodb-query-to-update-specific-fields-in-multiple-documents-in-a-collection

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
