In this example, we will write and execute a PHP script that queries all the sales records that are older than one month, migrate them to a MongoDB collection, and have them removed from the sales table.
1. Open up the text editor, create a new file and put the following code in it:
<?php require 'mysql.php'; require 'dbconnection.php'; $cutoffDate = date('Y-m-d', strtotime('-30 day')); $mysql = getMySQLConnection(); //get all the sales records older than one month $query = sprintf("SELECT * FROM sales WHERE". "DATE(time_of_sales) < '%s'", $cutoffDate); printf("Fetching old data from MySQL...\n"); $result = $mysql->query($query); if($result === False) { die(sprintf("Error executing query %s" % $mysql->error)); } printf("Migrating to MongoDB...\n"); $mongo = DBConnection::instantiate(); $collection = $mongo->getCollection('sales_archive'); while($record = $result->fetch_assoc()) { try{ $collection->insert($record...