Home >Backend Development >PHP Tutorial >How to achieve real-time data synchronization in MongoDB using PHP
How to use PHP to achieve real-time data synchronization in MongoDB
Install PHP and enable the MongoDB driver. Run the following command in the terminal to install dependencies:
sudo pecl install mongodb
Connect to MongoDB
First, we need to write code in PHP to connect to MongoDB. The following is a simple example:
<?php $mongoUrl = "mongodb://localhost:27017"; $mongoClient = new MongoDBClient($mongoUrl); ?>
Get the collections in the source database and target database
Before data synchronization, we need to get the collections in the source database and target database. The following is a simple example:
<?php $sourceDB = $mongoClient->selectDatabase("source_database"); $targetDB = $mongoClient->selectDatabase("target_database"); $sourceCollection = $sourceDB->selectCollection("source_collection"); $targetCollection = $targetDB->selectCollection("target_collection"); ?>
Listen for changes in the source database
In order to achieve real-time data synchronization, we need to listen for changes in the source database. MongoDB provides the Change Streams feature, which captures any change operations on the database and transmits them to us as streaming events. The following is an example of listening for changes in the source database:
<?php $changeStream = $sourceCollection->watch(); foreach ($changeStream as $change) { // 处理变更事件 } ?>
According to Operation type, we can perform corresponding data synchronization operations. The following is a simple example:
<?php foreach ($changeStream as $change) { $operationType = $change['operationType']; switch ($operationType) { case "insert": // 获取插入的文档 $insertedDocument = $change['fullDocument']; // 将文档插入目标数据库 $targetCollection->insertOne($insertedDocument); break; case "update": // 获取更新后的文档 $updatedDocument = $change['fullDocument']; // 获取更新前的文档 $previousDocument = $change['documentKey']; // 更新目标数据库中的文档 $targetCollection->replaceOne(['_id' => $previousDocument], $updatedDocument); break; case "delete": // 获取删除的文档 $deletedDocument = $change['documentKey']; // 删除目标数据库中的文档 $targetCollection->deleteOne(['_id' => $deletedDocument]); break; } } ?>
Full example
The following is a complete sample code showing how to achieve real-time data synchronization in MongoDB using PHP:
<?php $mongoUrl = "mongodb://localhost:27017"; $mongoClient = new MongoDBClient($mongoUrl); $sourceDB = $mongoClient->selectDatabase("source_database"); $targetDB = $mongoClient->selectDatabase("target_database"); $sourceCollection = $sourceDB->selectCollection("source_collection"); $targetCollection = $targetDB->selectCollection("target_collection"); $changeStream = $sourceCollection->watch(); foreach ($changeStream as $change) { $operationType = $change['operationType']; switch ($operationType) { case "insert": $insertedDocument = $change['fullDocument']; $targetCollection->insertOne($insertedDocument); break; case "update": $updatedDocument = $change['fullDocument']; $previousDocument = $change['documentKey']; $targetCollection->replaceOne(['_id' => $previousDocument], $updatedDocument); break; case "delete": $deletedDocument = $change['documentKey']; $targetCollection->deleteOne(['_id' => $deletedDocument]); break; } } ?>
Summary
This article introduces how to use the PHP programming language to achieve real-time data synchronization in MongoDB. Real-time data synchronization between two MongoDB databases can be achieved by listening to change events in the source database and performing corresponding data synchronization operations according to different operation types. In addition, we also provide a complete sample code to help readers better understand and apply this technology. To learn more about MongoDB and PHP, see the official documentation and tutorials.
The above is the detailed content of How to achieve real-time data synchronization in MongoDB using PHP. For more information, please follow other related articles on the PHP Chinese website!