Saturday, August 1, 2015

Rename Database using Copy then delete method in MongoDB

Simplest way to rename a MongoDB database using MongoDB CLI:

db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();

Fastest way to install MongoDB PHP Extension / Driver

By far the fastest and easiest way to install MongoDB PHP Extension (upgradable) via command line.

pecl install mongo 

yep, that is it!

In the future you can upgrade it to newer versions using:

pecl upgrade mongo

Please make sure to use only STABLE version.

Simple MongoDB database restore command line script

MONGODB RESTORE Database / Collection

ENTIRE DATABASE or a particular COLLECTION

Command below will restore entire database from directory mongodb-db_name-YYYYMMDD

mongorestore --host 127.0.0.1 --port 27017 /your_backup_dir/mongodb-20140213




Simple MongoDB BackUp command line script

MONGODB BACKUP EXAMPLE

Backing Up Entire Database:

Command below will backup entire database + create a directory mongodb-db_name-YYYYMMDD

mongodump --host 127.0.0.1:27017 -d db_name --out /your_backup_dir/mongodb-db_name-YYYYMMDD >> /dev/null

Backing Up Collection inside a Database:

Command below will backup a collection inside a particular database + create a directory mongodb-collection_name-YYYYMMDD

mongodump --host 127.0.0.1:27017 -d db_name -c collection_name --out /your_backup_dir/mongodb-db_name-collection_name-YYYYMMDD  >> /dev/null



[SOLVED] MongoDB error: The MongoCursor object has not been correctly initialized by its constructor (PHP Driver)

I have been plagued by this error message intermittently for about 6-months.

PHP Fatal error:  Uncaught exception 'MongoException' with message 'The MongoCursor object has not been correctly initialized by its constructor' in /data_local/app/dff/inc/DffDataObj_db_mongo.class.php:37

Specifically "The MongoCursor object has not been correctly initialized by its constructor"

 I have searched on Google off and on but never found any good solution.

Finally I accidentally found the solution, because I installed another web server which has a newer MongoDB PHP Extension Driver.

Instantly the problem is gone.

It turns out culprit was the PHP Extension version that had the problem was 1.5.0, I got that from PECL. The error usually happen when a MongoDB connection has been opened for at least 0.5 second and that connection was either used in loop doing multiple queries to Mongo, or it was used to query multiple collections. In any case, the error was somewhat intermittent and hard to debug.

The solution and how-to fix this problem is very simple, you simply need to upgrade it.

Type this on your shell:

pecl list

That should tell you all PHP packages you have installed using PECL.

Mine looks like this:

Installed packages, channel pecl.php.net:
=========================================
Package Version State
mongo   1.6.10  stable

But before I upgraded that used to say 1.5.0

Anyways here is the command I used to upgrade to latest stable version:

pecl upgrade mongo

After upgrade process completed, you can run

pecl list 

again to double check to make sure your version has been upgraded.

and you can also make sure on PHP info using the following command:

php -i

(look for mongo section and check the version there)

That is all... I hope that help some one like me! :-)