tencent cloud

Batch Data Update Example
Last updated:2025-11-24 17:13:41
Batch Data Update Example
Last updated: 2025-11-24 17:13:41
This document describes the operation process of key-based flashback and data update based on the timeline shown in the following figure.


Data Preparation

1. The original collection name is test. At time point A, there are three data records in the table, recording the scores of three people, all of which are 90 points.

2. At time point C, the scores of the three people in the collection test are updated, and information about David is added.


Detecting Issues and Initiating Key-based Flashback

At time point D, it is found that the scores of Tom, Jerry, and David are abnormal, while the data of Harry is normal. To precisely fix this issue, we plan to restore the data status of these three people to time point B. By using the key-based flashback feature, with the unique _id field as the filter condition, you can enter the ID values of the three people to initiate a targeted restoration task. As shown in the following figure, select _id from the Specify Key list, enter the _id information (such as ObjectId("652d2a6505726f***********")) on separate lines in the Enter the value input box, and initiate a key-based flashback task. For specific operations, see Initiating a Key-based Flashback Task.

Data Update Steps

After the key-based flashback task is complete, update data based on the following steps:

Step 1: Confirming Data Integrity and Availability in the Flashback Collection, and Whether a Missing Record Collection at the Target Time Point Exists

1. Run show collections to check whether the flashback collection or missing record collection is generated. As shown in the following figure, two collections are generated.
test_bak1016151413 is the flashback collection, which stores the data snapshots corresponding to the specified keys that existed at the target time point B.
test_bak_missing1016151413 is the missing record collection, which records the specified keys that were not inserted at time point B but existed at time point D.

Note:
The time at the end of the flashback collection name and the missing record collection name is the operation time instead of the target rollback time.
2. Calculate the number of data records in the original collection test, the flashback collection test_bak1016151413, and the missing record collection test_bak_missing1016151413 based on key-based filter conditions.
As shown in the following figure, the flashback collection contains two data records, and the missing record collection contains one data record. The three data records have the same _id fields as the data records in the original collection, and the total quantity also meets expectations.



Step 2: Comparing Data in the Flashback Collection with That in the Original Collection, Confirming the Final List of IDs and Keys to Be Replaced in the Original Collection, and Filtering Out Unnecessary Records

View data in the flashback collection, check whether it meets the key-based filter conditions and the data requirements for restoring to target time point B, and select the data records that need to be replaced in the original collection. As shown in the following figure, two data records in the flashback collection meet the requirements at time point B.


Step 3: Matching Documents Based on the Selected Key List and Writing Them Back to the Original Collection Using the Upsert Method Based on the Document ID and Key

1. Run forEach to update the documents corresponding to _id in the flashback 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 data in the original collection meets expectations. As shown in the following figure, the data of Tom and Jerry has been updated to the status at time point B, meeting expectations.


Step 4: Checking Whether to Delete Corresponding Records in the Original Collection If a Missing Record Collection Is Available

If a missing record collection is available, data corresponding to the IDs in this collection does not exist at the target time point. Check whether to delete the corresponding ID data in the original collection based on the actual situation. As shown in the following figure, the data of David does not exist at time point B. If the data needs to be cleared, perform the data deletion operation.

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

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback