The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

Batch Update Data Example

Last updated: 2025-02-08 10:38:07

This article explains the data update operation process based on the timeline shown below and the key-based rollback.


Data Preparations

1. If the original collection name is test, at time point A, there are three pieces of data in the table, recording three people's scores (score), all of which are 90 points.

2. At time point C, the scores (score) of the three people in the collection test were updated, and zhao liu's information was added.


Detecting Issues and Initiating Key-Based Rollback

At time point D, it was discovered that the scores of Zhang San, Li Si, and Zhao Liu had problems, while Wang Wu's score had no problems. To solve this problem, it was planned to restore the information of Zhang San, Li Si, and Zhao Liu to time point B, which is between time points A and C. Therefore, the _id field of Zhang San, Li Si, and Zhao Liu was used as the filter condition for key-based rollback, and the key-based rollback feature was used to rollback the data to the target time point. Specifically, select _id from the Specifying Key list as shown below, enter the following information in the Input Parameter box line by line, and initiate a key-based rollback task. ObjectId("652d2a6505726f70625ce5cf") ObjectId("652d2a6b05726f70625ce5d0") ObjectId("652d2beb05726f70625ce5d2")


Updating Data Steps

Waiting for Key-Based Rollback Task Execution to Complete, Follow These Steps to Update Data

Step 1: Confirming Data in Rollback Collection is Complete and Usable, and Whether There is a Missing Records Collection for the Target Time Point

1. Execute show collections to check if a rollback collection or a missing records collection has been generated. As shown below, test_bak1016151413 is the rollback collection, and test_bak_missing1016151413 is the missing records collection. This indicates that at the target rollback time point B, there was data for the specified key that had not yet been written (i.e., there was no data for Zhao Liu at this time), otherwise, there would only be a rollback collection.

Note:
Note: The time at the end of the rollback collection name and the missing records collection name is the operation time, not the target rollback time.
2. Using key-based filter conditions, respectively calculate the number of data in the original test collection, the test_bak1016151413 rollback collection, and the test_bak_missing1016151413 missing collection.
As shown below, there are 2 in the rollback collection and 1 in the missing records collection. The three pieces of data have consistent _id fields with the data in the original collection, and the total number also meets expectations.



Step 2: Comparing Data in Rollback Collection and Original Collection to Confirm the Final IDs and Corresponding Keys to Replace Back to the Original Collection, While Filtering Out Unnecessary Records

Check the data in the rollback collection to confirm whether it meets key-based filter conditions and the requirements of returning to target time point B, and select the IDs and corresponding keys of the data to be replaced back to the original collection. As shown below, the 2 pieces of data in the rollback collection meet the requirements of time point B.


Step 3: Writing Back Matching Documents to the Original Collection by ID and Corresponding Keys Through Upsert Method Based on the Selected Key List

1. Use the forEach command to update the documents corresponding to _id in the rollback collection test_bak1016151413 to the original collection test.
db.test_bak1016151413.find({ _id: { $in: [ObjectId("652d2a6505726f70625ce5cf"), ObjectId("652d2a6b05726f70625ce5d0")] } }).forEach(function (doc) { db.test.updateOne({ _id: doc._id }, { $set: doc }, { upsert: true }); });
2. Check that the data in the original collection meets expectations. As shown below, the data for Zhang San and Li Si has been updated back to the state of time point B, meeting expectations.


Step 4: Deciding Whether to Delete Corresponding Records in the Original Collection if There is a Missing Records Collection

If there is a missing records collection, it indicates that the target time point does not yet have data for the corresponding IDs in that collection. Depending on the situation, consider whether to delete the corresponding ID data in the original collection. As shown below, Zhao Liu's data does not exist at time point B. If it is determined that this data needs to be cleaned, then perform the data deletion operation.

After deletion, check that the data in the original collection meets expectations.