example-data.md 1.17 KB
Newer Older
1
# Example Data
2

3
Some examples in this documentation use example data fixtures from
4 5
[zips.json][zips]. This is a dataset comprised of United States postal codes,
populations, and geographic locations.
6

7
Importing the dataset into MongoDB can be done in several ways. The following
8 9 10 11
example uses [mongodb extension][ext-mongodb]:

[zips]: http://media.mongodb.org/zips.json
[ext-mongodb]: http://php.net/mongodb
12

13
```
14 15
<?php

16
$file = 'http://media.mongodb.org/zips.json';
17 18
$zips = file($file, FILE_IGNORE_NEW_LINES);

19
$bulk = new MongoDB\Driver\BulkWrite;
20

21
foreach ($zips as $string) {
22
    $document = json_decode($string);
23
    $bulk->insert($document);
24 25
}

26
$manager = new MongoDB\Driver\Manager('mongodb://localhost');
27

28
$result = $manager->executeBulkWrite('demo.zips', $bulk);
29 30 31
printf("Inserted %d documents\n", $result->getInsertedCount());
```

32
Executing this script should yield the following output:
33 34 35 36

```
Inserted 29353 documents
```
37

38 39
You may also import the dataset using the [mongoimport][mongoimport] command,
which is included with MongoDB:
40

41 42 43
[mongoimport]: http://docs.mongodb.org/manual/reference/program/mongoimport/

```bash
44
$ mongoimport --db demo --collection zips --file zips.json --drop
45
```