Docs Menu
Docs Home
/ / /
PHP Library Manual

Example Data

Some examples in this documentation use example data fixtures from zips.json and primer-dataset.json.

Importing the dataset into MongoDB can be done in several ways. The following example imports the zips.json file into a test.zips collection using the extension directly:

$filename = '';
$lines = file($filename, FILE_IGNORE_NEW_LINES);
$bulk = new MongoDB\Driver\BulkWrite;
foreach ($lines as $line) {
$bson = MongoDB\BSON\fromJSON($line);
$document = MongoDB\BSON\toPHP($bson);
$manager = new MongoDB\Driver\Manager('mongodb://');
$result = $manager->executeBulkWrite('test.zips', $bulk);
printf("Inserted %d documents\n", $result->getInsertedCount());

The output would then resemble:

Inserted 29353 documents

You may also import the datasets using mongoimport, which is included with MongoDB:

mongoimport --db test --collection zips --file zips.json --drop
mongoimport --db test --collection restaurants --file primer-dataset.json --drop
← Tailable Cursor Iteration