How to mongoimport in a remote server
Asked Answered
S

3

15

I'm trying to create a simple website that displays a table based on a relatively small json data (>5MB). I plan on importing the json into MongoDB and performing queries using a driver (either Mongoose or PyMongo). As the data size is very small, I'd like to avoid using the cloud service so that I only pay for the server costs.

I've tried to familiarize myself with mongoimport by trying that on my local machine. I was successful in importing the data locally but now I'd like to do this on the remote server.

How would you do this on a remote server? As I understand, mongod should be running in the background to start mongo or mongoimport. How do you achieve this with one window? Is there a guide that any of you found helpful in doing this type of a job?

Salsbury answered 9/7, 2019 at 3:54 Comment(0)
T
15

Since your data size is small, I'd recommend using MongoDB Atlas. Create a cluster and mongoimport using the URI which can be found within your cluster (Connect --> Connect Your Application --> Connection String Only).

To mongoimport,

mongoimport --uri "URI" --drop --collection collectionName --file localFileLocation

where you need to specify: 1) "URI" 2) collectionName 3) localFileLocation.

Once the mongoimport works, you can query the cloud data via Mongo shell after fetching the connection string (Connect --> Connect with the Mongo Shell --> Connection String Only)

mongo "connectionString" --username yourUsername

where you need to specify: 1) "connectionString" 2) yourUsername.

Now, type in your password associated with the cluster you created, then you should be connected and ready to query data held in Mongo Atlas from the command line.

Tonnie answered 20/7, 2019 at 20:21 Comment(0)
M
6

To import a csv file from your local machine to remote server:

mongoimport --host "hostname" --port "port" --db "databasename" --collection "collectionName" --file "filePath" --type csv --headerline

for importing json file

mongoimport --host "hostname" --port "port" --db "databasename" --collection "collectionName" --file "filePath"

--host Specifies hostname (In your case it is the address of your remote server) by default it is localhost.

--port Specifies port on which mongo server is running in your remote server by default it is 27017.

--file Specifies the location and name of a file containing the data to import.

Manipur answered 9/7, 2019 at 7:44 Comment(0)
P
3

You can directly use the official mongoimport command, from a shell.

Below a full example :

mongoimport -h localhost:27018 -d developer-database -c developer-collection -u root -p root --authenticationDatabase admin --file data-local.json

You can add the option --jsonArray if you json contains an array.

You can add the option --type csv, if your file is not a json but a csv.

More informations: https://docs.mongodb.com/manual/reference/program/mongoimport/#bin.mongoimport

Previdi answered 27/10, 2019 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.