web analytics

PHP Script to create a test database, collection and data in mongo for testing purpose

If you need an immediate mongo database with data for test purpose, you can use the following script.

The script to be put in a nutshell – it will create a database named commandtest, create a collection named customer inside it. The collection customer has two fields – customerid, company

root@abc# vi createdb.php

<?php
$m = new MongoClient();    // connect
$db = $m->commandtest;            // create database commandtest
$chars = “abcdefghijklmnopqrstuvwxyz”;
$arraykeys = range(‘a’, ‘z’);
$collection = $db->customer;   // create collection customer
for($i=0;$i<100;$i++)
{
$string = substr(str_shuffle($chars), 0, 5);
$document = array(“customer_id” => $i.”_”.$string,”company” => $string );
$collection->insert($document);
}
?>

 

Now try executing the script from backend itself .

root@abc# php createdb.php

 

If the script executed without errors, you can check if it did what it actually should, by logging in to mongo shell and execute the following commands.

root@abc# mongo
MongoDB shell version: 2.4.5
connecting to: test
> show databases
admin   (empty)
commandtest     0.203125GB                —   your database is created
db_list 11.9482421875GB
db_list_test    1.953125GB
dev     0.078125GB
dummy_data      13.947265625GB
local   0.078125GB
> use commandtest                                —-   switch to your database commandtest
switched to db commandtest
> show collections                                  —-         show tables/collections in it
customer                                                  —-         collection named customer is present
system.indexes
> db.customer.find()                               —–      show data inside the collection commandtest
{ “_i d” : ObjectId(“5212f046bc1eef2112138ff4”), “customer_id” : “0_stinh”, “company” : “stinh” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ff5”), “customer_id” : “1_rhksp”, “company” : “rhksp” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ff6”), “customer_id” : “2_pscnf”, “company” : “pscnf” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ff7”), “customer_id” : “3_nhouy”, “company” : “nhouy” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ff8”), “customer_id” : “4_hxrlc”, “company” : “hxrlc” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ff9”), “customer_id” : “5_mvath”, “company” : “mvath” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ffa”), “customer_id” : “6_ylhrt”, “company” : “ylhrt” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ffb”), “customer_id” : “7_hnusq”, “company” : “hnusq” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ffc”), “customer_id” : “8_gnfwh”, “company” : “gnfwh” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ffd”), “customer_id” : “9_cmhey”, “company” : “cmhey” }
{ “_id” : ObjectId(“5212f046bc1eef2112138ffe”), “customer_id” : “10_pomdi”, “company” : “pomdi” }
{ “_id” : ObjectId(“5212f046bc1eef2112138fff”), “customer_id” : “11_uwmgy”, “company” : “uwmgy” }
{ “_id” : ObjectId(“5212f046bc1eef2112139000”), “customer_id” : “12_lfgke”, “company” : “lfgke” }
{ “_id” : ObjectId(“5212f046bc1eef2112139001”), “customer_id” : “13_dnbjv”, “company” : “dnbjv” }
{ “_id” : ObjectId(“5212f046bc1eef2112139002”), “customer_id” : “14_xziqf”, “company” : “xziqf” }
{ “_id” : ObjectId(“5212f046bc1eef2112139003”), “customer_id” : “15_trksx”, “company” : “trksx” }
{ “_id” : ObjectId(“5212f046bc1eef2112139004”), “customer_id” : “16_jmxkh”, “company” : “jmxkh” }
{ “_id” : ObjectId(“5212f046bc1eef2112139005”), “customer_id” : “17_csybp”, “company” : “csybp” }
{ “_id” : ObjectId(“5212f046bc1eef2112139006”), “customer_id” : “18_nquae”, “company” : “nquae” }
{ “_id” : ObjectId(“5212f046bc1eef2112139007”), “customer_id” : “19_xofhb”, “company” : “xofhb” }
Type “it” for more

 

Hope this helps !!!!

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>