Can I update multiple properties?

QuestionsQuestions8 SkillsProJavaScript ObjectsNov, 22 2025
0104

Yes, you can update multiple properties in a document using the updateOne() or updateMany() methods in MongoDB. You can use the $set operator to specify the fields you want to update. Here’s an example:

// Update a single document
db.collection.updateOne(
  { _id: documentId }, // Filter to find the document
  {
    $set: {
      property1: newValue1,
      property2: newValue2
    }
  }
);

// Update multiple documents
db.collection.updateMany(
  { condition }, // Filter to find the documents
  {
    $set: {
      property1: newValue1,
      property2: newValue2
    }
  }
);

In this example, replace collection with your collection name, documentId with the ID of the document you want to update, condition with the criteria for selecting multiple documents, and property1, property2, newValue1, and newValue2 with the actual property names and their new values.

0 Comments

no data
Be the first to share your comment!