Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • MongoDB - Get Started
  • What is MongoDB?
  • Install MongoDB
  • MongoDB Server
  • MongoDB Shell
  • MongoDB Shell Commands
  • MongoDB Compass
  • MongoDB Database
  • MongoDB Collections
  • MongoDB Documents
  • Insert Single Document
  • Insert Multiple Documents
  • Import Data into Collection
  • Find Single Document
  • Find Multiple Documents
  • MongoDB Cursor
  • MongoDB Sort Documents
  • Update Single Document
  • Update Multiple Documents
  • Update Arrays
  • Update Embedded Documents
  • Delete Documents
  • Relations in MongoDB
  • Aggregation in MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Import Data in MongoDB using Mongoimport

Here you are going to learn how to import JSON data or CSV file into a collection in MongoDB.

Use mongoimport command to import data into a collection. You should have installed the MongoDB database tools to use the mongoimport command.

To install the database tools, visit Database tools and download the zip file for your platform.

Now, extract and copy all .exe files and paste them to the MongoDB bin folder. On Windows, it is C:\Program Files\MongoDB\Server\<version>\bin folder.

Now, open the terminal or command prompt and navigate to the location where you have the JSON file to import so that you don't need to specify the whole path.

The following is the mongoimport command.

mongoimport --db database_name --collection collection_name ^ --authenticationDatabase admin --username <user> --password <password> ^ --file file_path

Now, execute the following command to import data from D:\MyData\employeesdata.json file to employees collection

D:\MyData> mongoimport --db test --collection employees --file employeesdata.json --jsonArray

The above command will import data into the employees collection in the test database. Note that --jsonArray indicates that the data in a file contains in an array.

Import Data from CSV File

Consider that you have D:\employeesdata.csv file which you want to import into new employee collection. Execute the following command to import data from the CSV file.

D:\MyData> mongoimport --db test --collection employeesdata --type csv --file employees.csv --fields _id,firstName,lastName

The --fields option indicates the field names to be used for each column in the CSV file. If a file contains the header row that should be used as a field name then use --headerline option instead of --fields. The above command will insert all data into employees collection, as shown below.

test&gt; db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.