Last active
December 21, 2015 05:19
-
-
Save kozmonaut/6256197 to your computer and use it in GitHub Desktop.
Some sample data for mongoDB.
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 | |
// Define connection and name of database/collection for storing data | |
try{ | |
$mongo = new Mongo(); | |
$db = $mongo->myfirstdb; | |
$collection = $db->samples; | |
}catch(MongoConnectionException $check){ | |
die('Database connection failed!'. $check->getMessage()); | |
} | |
//Creating arrays | |
$author = array('Bob Mizer','John Vogel','Mike Broom','George Kropp','Michael Barton'); // Some random names | |
$book = array('Book One','Book Two', 'Book Three', 'Book Four'); | |
$isbn = array('278-1-16592-479-1-74495','371-1-56592-479-4-24495','476-1-56592-479-2-84495','772-1-56592-479-9-34495'); | |
$content = array('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean tellus magna, gravida id mi sit amet, pulvinar imperdiet diam. Sed laoreet blandit leo, eu dignissim lorem lobortis in. Nunc rutrum eros viverra, lobortis urna quis, pharetra dolor.'); | |
//Generate random arrays | |
function RandomArray($array){ | |
$length = count($array); //Count elements in array | |
$generateIndex = mt_rand (0,$length-1); //Using mt_rand for randomize arrays | |
return $array[$generateIndex]; | |
} | |
// Return date time for each document | |
function RandomDateTime(){ | |
$generateNumber = mt_rand(0,6)*-1; | |
return strtotime($generateNumber); //Turn generate number into timestamp | |
} | |
// Create random documents | |
function documentCreate(){ | |
global $author,$book,$isbn,$content; | |
$authors = RandomArray($author); | |
$books = RandomArray($book); | |
$isbns = RandomArray($isbn); | |
$contents = RandomArray($content); | |
$date = new MongoDate(RandomDateTime()); | |
return array('authors'=>$authors,'books'=>$books,'isbns'=>$isbns,'contents'=>$contents,'date'=> $date); | |
} | |
for ($i=0;$i<1000000000;$i++) { // Create 500 random documents | |
$doc = documentCreate(); | |
$collection->insert($doc); // Insert into collection | |
} | |
echo 'Generating sample data into collection '.$collection.' <br></br>Sample data imported succesfully!'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment