Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

杨树贤 / ucenter

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • ucenter
  • ..
  • tutorial
  • example-data.txt
example-data.txt 1.35 KB
杨树贤's avatar
初始化项目
130b5f67
 
杨树贤 committed a year ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
============
Example Data
============

.. default-domain:: mongodb

Some examples in this documentation use example data fixtures from
`zips.json <https://media.mongodb.org/zips.json>`_ and
`primer-dataset.json <https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/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:
:php:`driver <mongodb>` directly:

.. code-block:: php

   <?php

   $filename = 'https://media.mongodb.org/zips.json';
   $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);
       $bulk->insert($document);
   }

   $manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/');

   $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 :manual:`mongoimport
</reference/program/mongoimport>`, which is included with MongoDB:

.. code-block:: sh

   mongoimport --db test --collection zips --file zips.json --drop
   mongoimport --db test --collection restaurants --file primer-dataset.json --drop