Summary: in this tutorial, you’ll learn how to use the
mongoimport tool to import a JSON file into a local MongoDB database server.
Download the JSON sample file
books.json file contains 431 documents about books.
Before importing it into your local MongoDB database server, you need to click the following download button:Download books.zip File
Note that the downloaded file is in the zip format, you need to extract it to
Import the books.json to MongoDB database sever
MongoDB installation comes with a very useful tool for importing data called
mongoimport located in the
First, open the Terminal on macOS and Linux or Command Prompt on Windows.
Second, use the following command to import the books.json to the MongoDB database server:
mongoimport c:\data\books.json -d bookdb -c books --drop
In this command:
- First, start with the
- Next, specify the path to the
books.jsondata file. In this example, it is
- Third, use
-d bookdbto specify the target database, which is
bookdbin this example.
- Fourth, use
-c booksto specify the target collection, which is
booksin this case.
- Finally, use the
--dropflag to drop the collection if it exists before importing the data.
If the import was successful, you will see the following output:
connected to: mongodb://localhost/ dropping: bookdb.books 431 document(s) imported successfully. 0 document(s) failed to import.
Third, type the following command from the terminal to connect to the
bookdb database on the local database using
Fourth, use the
count() method to return the number of documents in the
It returned 431 documents.
Finally, use the
find() method to return the document with the
_id 1 to examine the document:
In this tutorial, you’ve learned how to use the
mongoimport tool to import
books.json into the local MongoDB database server.