Javascript Script to Upload Csv File to Mongodb

Import Data into MongoDB¶

Author: MongoDB Documentation Team

In this guide, you will use the mongoimport tool distributed with MongoDB to bulk import data into your MongoDB instance.

Time required: 15 minutes

Cheque Your Environment¶

You will demand to ensure that your MongoDB case is running and accessible.

Check that you lot have an Atlas business relationship and accept deployed a MongoDB database cluster. See Get Started with Atlas for information on how to login and create a cluster in Atlas.

To make sure that your MongoDB instance is running on Windows, run the post-obit control from the Windows control prompt:

                      tasklist /FI                      "IMAGENAME eq mongod.exe"                    

If a mongod.exe instance is running, you volition come across something like:

                      Image Name                     PID Session Name        Session#    Mem Usage ========================= ======== ================ =========== ============ mongod.exe                    8716 Console                    1      nine,508 Thousand                    

To make sure your MongoDB instance is running on Mac, run the post-obit command from your concluding:

If a mongod case is running, you will see something like:

                                            89780                      ttys026                      0:53.48 ./mongod                    

To brand sure your MongoDB instance is running on Linux, run the post-obit control from your terminal:

If a mongod case is running, you volition see something like:

                                            89780                      ttys026                      0:53.48 ./mongod                    

Procedure¶

Download the sample data.¶

Y'all will be using a product inventory database for the Crud Getting Started guides. Download the inventory.crud.json file from GitHub.

Salve the downloaded file to a file named inventory.crud.json in your downloads folder. If you wish to save the file somewhere else, you can change the value of the --file choice to lucifer your file's location and proper noun.

Import the sample data into the inventory collection.¶

From your system crush or command prompt, use the mongoimport tool to insert the sample documents into the inventory drove in the test database. mongoimport is packaged with MongoDB and will exist in the /bin directory of the MongoDB repository.

Past default, mongoimport will import information into an instance of MongoDB on localhost , port 27017 . To import data into a MongoDB instance running on a unlike host or port, specify the hostname or port by including the --host and --port options.

Use the --drop option to drop the collection if it already exists. This ensures that the collection volition only contain the data you are importing.

Tip

For non-Atlas instances, supercede the <user> and <password> placeholders in the example with your MongoDB username and password. For all instances, update the --file path to signal to wherever you take saved your copy of the product inventory database.

Become to cloud.mongodb.com to login to your user account.

In the cluster console, click the "…" push button every bit pictured below.

../../_images/connect_panel.png

Select Command Line Tools from the menu.

Under the Import and Export Tools header, you volition see two copyable command line strings. Copy the one for mongoimport .

../../_images/importstring.png

You lot can use this command to run mongoimport by replacing the <DATABASE>, <PASSWORD>, <FILETYPE>, <COLLECTION> and <FILE> fields with the appropriate values.

In this example, <DATABASE> is exam , <FILETYPE> is JSON , <FILE> is inventory.crud.json (preceded with qualified path), and <Drove> is inventory .

                    mongoimport --db test --collection inventory                    ^                                                            --authenticationDatabase admin --username                    <user>                    --countersign                    <countersign>                    ^                                                            --driblet --file ~\downloads\inventory.crud.json                  
                    mongoimport --db                    test                    --collection inventory                    \                    --authenticationDatabase admin --username <user> --countersign <password>                    \                    --drop --file ~/Downloads/inventory.crud.json                  
                    mongoimport --db                    examination                    --collection inventory                    \                    --authenticationDatabase admin --username <user> --password <password>                    \                    --driblet --file ~/Downloads/inventory.crud.json                  

Summary¶

If you have successfully completed this guide, you have imported your first MongoDB data. In the side by side guide, you will call back the documents that you just imported.

What'southward Adjacent¶

In this guide, you will think all of the documents in a collection in your MongoDB database.

  • Read Data From MongoDB

pattersondidettioners.blogspot.com

Source: https://www.mongodb.com/docs/guides/server/import/

0 Response to "Javascript Script to Upload Csv File to Mongodb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel