Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created August 12, 2014 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmikola/9ab33101944a10f79a96 to your computer and use it in GitHub Desktop.
Save jmikola/9ab33101944a10f79a96 to your computer and use it in GitHub Desktop.
Inserting randomized fixture data into MongoDB
{
"require": {
"ext-mongo": "~1.5",
"fzaninotto/faker": "~1.0"
}
}
<?php
for ($i = 0; $i < 10; $i++) {
if ( ! pcntl_fork()) {
break;
}
}
require_once __DIR__ . "/vendor/autoload.php";
$faker = Faker\Factory::create();
$mc = new MongoClient();
$coll = $mc->test->php1164;
for ($i = 0; $i < 10000; $i++) {
if ($i % 1000 === 0) {
if (isset($batch)) {
printf("%s\n", json_encode($batch->execute()));
}
$batch = new MongoInsertBatch($coll);
}
$batch->add(['text' => $faker->paragraphs(5)]);
}
if (isset($batch)) {
printf("%s\n", json_encode($batch->execute()));
}
$result = $batch->execute();
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment