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:

Command-line option

Description

--type

This specifies that the type of the input file is CSV. It defaults to JSON; another possible value being TSV.

-d

This is the target database in which the data will be loaded.

-c

This is the collection in the previously mentioned database in which the data will be loaded.

--headerline

This is relevant only in case of TSV or CSV files. It indicates that the first line of the file is the header. The same names would be used as the name of the fields in the document.

--drop

Drop the collection before importing data.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset