Search data from MongoDB database, also delete records from MongoDB database based on records found in CSV file and add data to MongoDB from CSV File
Search Data from DND
<?php
///Author Sandip Dutta [i.sandip@gmail.com]///
///Command Line Use php csv_mongodb_search.php 9007935611
error_reporting(0); //set all error reporting to false
////Parsing Ini files to read the values ////
$data=parse_ini_file('settings.ini');
$mongo_username=$data['mongo_username'];
$mongo_password=$data['mongo_password'];
$mongo_db=$data['mongo_db'];
$mongo_collection=$data['mongo_collection'];
$csv_file=$data['csv_file'];
///Connecting MongoDB
$m = new MongoClient("mongodb://localhost", array("username" => $mongo_username, "password" => $mongo_password));
$db = $m->selectDB($mongo_db);
$collection = new MongoCollection($db, $mongo_collection);
$number=intval($argv[1]);
$Numbers = array("Phone_Numbers" => $number);
$cursor = $collection->find($Numbers);
foreach ( $cursor as $value )
{
var_dump( $value );
}
?>
Output
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "5ad0c47d8d10f7e4afe7ca2f"
}
["Phone_Numbers"]=>
int(9007935611)
["Description"]=>
string(8) "12-1-A-2"
}
-------------------------------------------------------------
Delete all records from MongoDB based on CSV file
<?php
///Author Sandip Dutta [i.sandip@gmail.com]///
error_reporting(0); //set all error reporting to false
////Parsing Ini files to read the values ////
$data=parse_ini_file('settings.ini');
$mongo_username=$data['mongo_username'];
$mongo_password=$data['mongo_password'];
$mongo_db=$data['mongo_db'];
$mongo_collection=$data['mongo_collection'];
$csv_file=$data['csv_file'];
///Connecting MongoDB
$m = new MongoClient("mongodb://localhost", array("username" => $mongo_username, "password" => $mongo_password));
$db = $m->selectDB($mongo_db);
$collection = new MongoCollection($db, $mongo_collection);
///Reading CSV file first Time to delete all records from Mongo DB
$file = fopen($csv_file, 'r');
$i=0;
while (($line = fgetcsv($file)) !== FALSE) {
if($i==0)
{
///Continue for the header in csv file
$i++;
continue;
}
// /print_r($line[1]);
echo $numberfromcsv=$line[1];
$numberfromcsv=intval($numberfromcsv);
$Numbers = array("Phone_Numbers" => $numberfromcsv);
$cursor = $collection->remove($Numbers);
}
fclose($file);
?>
Add Data to MongoDB from CSV file based on conditions
<?php
///Author Sandip Dutta [i.sandip@gmail.com]///
error_reporting(0); //set all error reporting to false
////Parsing Ini files to read the values ////
$data=parse_ini_file('settings.ini');
$mongo_username=$data['mongo_username'];
$mongo_password=$data['mongo_password'];
$mongo_db=$data['mongo_db'];
$mongo_collection=$data['mongo_collection'];
$csv_file=$data['csv_file'];
///Connecting MongoDB
$m = new MongoClient("mongodb://localhost", array("username" => $mongo_username, "password" => $mongo_password));
$db = $m->selectDB($mongo_db);
$collection = new MongoCollection($db, $mongo_collection);
///Reading CSV file first Time to delete all records from Mongo DB
$file = fopen($csv_file, 'r');
$i=0;
while (($line = fgetcsv($file)) !== FALSE) {
if($i==0)
{
///Continue for the header in csv file
$i++;
continue;
}
$numberfromcsv=$line[1];
if($line[3]=='A')
{
$Description=$line[0].'-'.$line[2].'-'.$line[3].'-'.$line[4];
echo 'Adding Number to DB : '.$numberfromcsv.':'.$Description.PHP_EOL;
$obj = array('Phone_Numbers' => intval($numberfromcsv),'Description' => $Description);
$collection->insert($obj);
}
$i++;
}
fclose($file);
?>