- Python Business Intelligence Cookbook
- Robert Dempsey
- 304字
- 2021-07-30 09:51:50
Importing a CSV file into MongoDB
Importing data from a CSV file into MongoDB is one of the fastest methods of import available. It is also one of the easiest. With almost every database system exporting to CSV, the following recipe is sure to come in handy.
Getting ready
The UK Road Safety Data comprises three CSV files: accidents7904.csv
, casualty7904.csv
, and vehicles7904.csv
. Use this recipe to import the Accidents7904.csv
file into MongoDB.
How to do it…
Run the following command at the command line:
./Applications/mongodb-3.0.4/bin/mongoimport --db pythonbicookbook --collection accidents --type csv --headerline --file '/Data/Stats19-Data1979-2004/Accidents7904.csv' --numInsertionWorkers 5
After running that command, you should see something similar to the following screenshot:

The following command is what you would use for Windows:
C:\Program Files\MongoDB\Server\3.0\bin\mongoimport --db pythonbicookbook --collection accidents --type csv --headerline --file C:\Data\Stats19-Data1979-2004\Accidents7904.csv --numInsertionWorkers 5
How it works…
Each part of the command has a specific function:
./Application/mongodb-3.0.4/mongoimport
: The MongoDB utility that we will use to import the data.-- db pythonbicookbook
: Specifies the name of the database to use.-- collection accidents
: Tells the tool the name of the collection to use; if the collection doesn't exist, it will be created automatically.--type csv
: Specifies that we're importing a CSV file.--headerline
: Tells the import tool that our CSV file has aheaderline
containing the column headers.--file '/Data/Stats19-Data1979-2004/Accidents7904.csv'
: The full path to the file which contains the data that we're importing.--numInsertionWorkers 5
: By default, MongoDB uses a single worker to import the data. To speed this up, we specify the use of5
workers.
There's more…
You can import data from another computer in MongoDB:
Tip
Importing into a MongoDB instance running on another computer
If you need to import data into a non-local instance of MongoDB, use the --host
<hostname><:port>
options. Otherwise, mongoimport
assumes that you are importing into a local MongoDB instance.
- Apache ZooKeeper Essentials
- C程序設計簡明教程(第二版)
- Oracle Database In-Memory(架構與實踐)
- Machine Learning with R Cookbook(Second Edition)
- Spring Boot+Spring Cloud+Vue+Element項目實戰:手把手教你開發權限管理系統
- Bulma必知必會
- Python Data Analysis(Second Edition)
- Learning Data Mining with R
- HTML5與CSS3基礎教程(第8版)
- Raspberry Pi Home Automation with Arduino(Second Edition)
- Odoo 10 Implementation Cookbook
- Hands-On Nuxt.js Web Development
- IDA Pro權威指南(第2版)
- RubyMotion iOS Develoment Essentials
- 從零開始學Android開發