Skip to content

Commit 47331cb

Browse files
committed
Add example data to tutorial
1 parent 830148b commit 47331cb

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

docs/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Tutorials
99
/tutorial/commands
1010
/tutorial/gridfs
1111
/tutorial/indexes
12+
/tutorial/example-data

docs/tutorial/example-data.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

docs/tutorial/example-data.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
============
2+
Example Data
3+
============
4+
5+
.. default-domain:: mongodb
6+
7+
Some examples in this documentation use example data fixtures from
8+
`zips.json <http://media.mongodb.org/zips.json>`_. This is a dataset comprised
9+
of United States postal codes, populations, and geographic locations.
10+
11+
Importing the dataset into MongoDB can be done in several ways. The following
12+
example uses the :php:`driver <mongodb>` directly:
13+
14+
.. code-block:: php
15+
16+
<?php
17+
18+
$file = 'http://media.mongodb.org/zips.json';
19+
$zips = file($file, FILE_IGNORE_NEW_LINES);
20+
21+
$bulk = new MongoDB\Driver\BulkWrite;
22+
23+
foreach ($zips as $string) {
24+
$document = json_decode($string);
25+
$bulk->insert($document);
26+
}
27+
28+
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/');
29+
30+
$result = $manager->executeBulkWrite('demo.zips', $bulk);
31+
printf("Inserted %d documents\n", $result->getInsertedCount());
32+
33+
The output would then resemble::
34+
35+
Inserted 29353 documents
36+
37+
You may also import the dataset using :manual:`mongoimport
38+
</reference/program/mongoimport>`, which is included with MongoDB:
39+
40+
.. code-block:: none
41+
42+
$ mongoimport --db demo --collection zips --file zips.json --drop

0 commit comments

Comments
 (0)