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

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.

主站蜘蛛池模板: 延长县| 八宿县| 沈阳市| 民乐县| 乌鲁木齐县| 扶余县| 横峰县| 封丘县| 忻州市| 平远县| 阳信县| 彭泽县| 永靖县| 古蔺县| 全椒县| 新和县| 夏津县| 嘉定区| 神木县| 石首市| 南江县| 阿巴嘎旗| 罗定市| 正定县| 滨海县| 太白县| 天镇县| 来安县| 邳州市| 淳安县| 衡阳市| 都江堰市| 苗栗市| 新化县| 万山特区| 甘泉县| 绵竹市| 宿州市| 卢湾区| 昌图县| 吉木萨尔县|