Created
March 1, 2016 12:45
-
-
Save rummykhan/171dd5a5818de9e297cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = null; | |
function db() | |
{ | |
global $host; | |
$m = new MongoClient("mongodb://$host:27017"); | |
return $m; | |
} | |
function fieldToLower($db, $collection, $field) | |
{ | |
$m = db(); | |
$d = $m->selectDB($db); | |
$c = $d->selectCollection($collection); | |
foreach ($c->find() as $key => $value) | |
{ | |
if(isset($value[$field])) | |
{ | |
$old = $value[$field]; | |
$c->update(['_id' => new MongoId($value['_id'])], ['$set' => [$field => strtolower($value[$field])]]); | |
$new = strtolower($value[$field]); | |
print("Changed $old ========> $new \n"); | |
} | |
} | |
} | |
function getInput($field) | |
{ | |
print("Enter $field Name :: "); | |
$handle = fopen ("php://stdin","r"); | |
$line = fgets($handle); | |
if(trim($line) != '') | |
return trim($line); | |
return null; | |
} | |
function connectToHost() | |
{ | |
global $host; | |
$host = getInput('Host IP'); | |
if(!is_null($host)) | |
{ | |
try | |
{ | |
$m = db(); | |
return true; | |
} | |
catch(MongoConnectionException $ex) | |
{ | |
print("\n"); | |
print("Unable to connect to datbase. \nGood Bye!"); | |
return false; | |
} | |
} | |
print("Sorry! Host cannot be null. \nGood Bye!"); | |
return false; | |
} | |
function init() | |
{ | |
if(!connectToHost()) | |
return; | |
$db = getInput('Database'); | |
if(!is_null($db)) | |
{ | |
$collection = getInput('Collection'); | |
if(!is_null($collection)) | |
{ | |
$field = getInput('Field'); | |
fieldToLower($db, $collection, $field); | |
} | |
} | |
} | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is a simple command line script for mongodb manager,
you can connect to any host, select collection, and set the field to lower case.