tencent cloud

TencentDB for MongoDB

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Strengths
Use Cases
Cluster Architecture
Product Specifications
Features
Regions and AZs
Terms
Service Regions and Service Providers
Purchase Guide
Billing Overview
MongoDB Pricing
Billing Formula
Payment Overdue
Backup Space Billing
Configuration Adjustment Billing
Getting Started
Quickly Creating an Instance
Connecting to a TencentDB for MongoDB Instance
Reading/Writing Database
Operation Guide
Access Management
Instance Management
Node Management
Version Upgrade
Network Configuration
Monitoring
Backup and Rollback
Database Audit
Data Security
SSL Authentication
Log Management
Database Management
Multi-AZ Deployment
Disaster Recovery/Read-Only Instances
Parameter Configuration
Recycle Bin
Task Management
Performance Optimization
Data Migration Guide
Practical Tutorial
Optimizing Indexes to Break Through Read/Write Performance Bottlenecks
Troubleshooting Mongos Load Imbalance in Sharded Cluster
Considerations for Using Shard Clusters
Sample of Reading and Writing Data in MongoDB Instance
Methods for Importing and Exporting Data Based on CVM Connected with MongoDB
What to Do for Errors of Repeated Instance Creation and Deletion of Databases with the Same Names?
Troubleshooting MongoDB Connection Failures
Shard Removal Task: Guide for Confirming the Progress and Troubleshooting Issues
Performance Fine-Tuning
Ops and Development Guide
Development Specifications
Command Support in Sharded Cluster v3.2
Command Support in v3.6
Development Ops
Troubleshooting
Increased Slow Queries
Number of Connections Exceeding Limit
API Documentation
History
Introduction
API Category
Making API Requests
Instance APIs
Backup APIs
Account APIs
Other APIs
Task APIs
Introduction
Data Types
Error Codes
Instance Connection
Shell Connection Sample
PHP Connection Sample
Node.js Connection Sample
Java Connection Sample
Python Connection Sample
Python Read/Write Sample
Go Connection Sample
PHP Reconnection Sample
Product Performance
Test Environment
Test Method
Test Result
FAQs
Cost
Features
Sharded Cluster
Instance
Rollback and Backup
Connection
Data Migration
Others
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us

Batch Data Update Example

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 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.


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백