官术网_书友最值得收藏!

Creating test data

This recipe is about creating test data for some of the recipes in this chapter and also for the later chapters in this book. We will demonstrate how to load a CSV file in a mongo database using the mongo import utility. This is a basic recipe, and if the reader is aware of the data import utility; they can just download the CSV file from the Packt website (pincodes.csv), load it in the collection by themselves, and skip the rest of the recipe. We will use the default database, test, and the collection will be named postalCodes.

Getting ready

The data used here is for postcodes in India. Download the pincodes.csv file from the Packt website. The file is a CSV file with 39,732 records; it should create 39,732 documents on successful import. We need to have the Mongo server up and running. Refer to the Installing single node MongoDB recipe from Chapter 1, Installing and Starting the Server for instructions on how to start the server. The server should begin listening for connections on the default port, 27017.

How to do it…

  1. Execute the following command from the shell with the file to be imported in the current directory:
    $ mongoimport --type csv -d test -c postalCodes --headerline --drop pincodes.csv
    
  2. Start the mongo shell by typing in mongo on the command prompt.
  3. In the shell, execute the following command:
    > db.postalCodes.count()
    

How it works…

Assuming that the server is up and running, the CSV file has been downloaded and is kept in a local directory where we execute the import utility with the file in the current directory. Let's look at the options given in the mongoimport utility and their meanings:

The final value on the command prompt after all the options are given is the name of the file, pincodes.csv.

If the import goes through successfully, you should see something similar to the following printed to the console:

2015-05-19T06:51:54.131+0000 connected to: localhost
2015-05-19T06:51:54.132+0000 dropping: test.postalCodes
2015-05-19T06:51:54.810+0000 imported 39732 documents

Finally, we start the mongo shell and find the count of the documents in the collection; it should indeed be 39,732 as seen in the preceding import log.

Note

The postal code data has been taken from https://github.com/kishorek/India-Codes/. This data is not taken from an official source and might not be accurate as it is being compiled manually for free public use.

See also

The Performing simple querying, projections, and pagination from Mongo shell recipe is about executing some basic queries on the data imported.

主站蜘蛛池模板: 桐庐县| 安化县| 万全县| 红原县| 南康市| 阿合奇县| 祁连县| 凤阳县| 天柱县| 崇礼县| 洪江市| 山阳县| 都安| 平罗县| 安国市| 晋中市| 来安县| 邓州市| 沭阳县| 新沂市| 厦门市| 苏尼特左旗| 汉寿县| 奉新县| 宁国市| 土默特左旗| 翁牛特旗| 大宁县| 中超| 鹤山市| 福建省| 河东区| 石河子市| 福州市| 临潭县| 汝州市| 西城区| 平江县| 定襄县| 常宁市| 吉水县|