Skip to content
Documentation
UpdateMany Method

Update Many JSON Method

The updateMany method allows you to modify multiple documents within a data set (JSON file) based on specified criteria. It's a powerful operation for making bulk updates to your data. Here's how you can use it:

Syntax

// Arrange
const dataname = "users";
const query = { age: { $gte: 25 } }; // Find documents with age greater than or equal to 25
const updateQuery = {
    $set: { name: "Updated Name" }, // Set the name field to "Updated Name"
    $inc: { age: 1 }, // Increment the age field by 1
    $addToSet: { hobbies: "Swimming" }, // Add "Swimming" to the hobbies array if not already present
    $push: { friends: "Alice" }, // Push "Alice" into the friends array
    $unset: { email: "" }, // Remove the email field
    $currentDate: { lastModified: true } // Set the lastModified field to the current date
};
 
// Act
const result = await db.updateMany(dataname, query, updateQuery);
 
// Results:
return {
  acknowledged: true,
  message: `${updatedCount} document(s) updated successfully.`,
  results: updatedDocument,
};

Operation Keys:

you can know more about the Operation Keys here

Parameters

  • dataname: The filename (without the .json extension) of the JSON file you want to update.
  • query: An object specifying the criteria for selecting documents to update (e.g., { age: { $gte: 25 } }).
  • updateQuery: An object containing the update operations to apply (e.g., $set, $inc, $addToSet, $push, $unset, $currentDate).

Return Value

The updateMany method returns an object with the following properties:

  • acknowledged: A boolean indicating whether the operation was successful (true) or not (false).
  • message: A descriptive message indicating the outcome of the update (e.g., "2 document(s) updated successfully.").
  • results: An array of updated documents (if needed).

Example

In your provided example:

  • We search for documents in the "users" data set where the age is greater than or equal to 25.
  • We apply various update operations to these documents, such as setting the name, incrementing the age, adding hobbies, pushing friends, removing the email, and updating the lastModified date.
  • The result indicates the number of documents updated and provides the updated document details.

Outcome

The updateMany method efficiently modifies multiple documents based on your specified criteria, allowing you to manage your data effectively within your verse.db project.


VERSE.DB. Powered by JEDI Studio.