DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Import Data from Files into Mongodb


From CSV File

Here I use mongoDB's mongoimport tool to import csv file into Meteor app's Mongodb.

First download mongodb package (mongodb-linux-x86_64-2.6.5.tgz) from mongoDB, extract it into ~/apps folder.

Next start the Meteor app. Open a new terminal window, in project root folder, run meteor mongo. You can see something like "connecting to: 127.0.0.1:3001/meteor". This tells us database server is listening on port 3001, and database name is "meteor".

Save the following codes into app's $HOME/private folder as importdb.sh:

mongoimport -h localhost:3001 --db meteor --collection fairs --type csv --file fairs.csv --headerline

Here "-d" equals to "--db", "-c" equals to "--collection". And the source csv file is saved in file private/fairs.csv.

It's headerline (first line of csv file) is also comma-seperated:

Name, Age, Job
Bob, 35, Cooker
Tom, 28, Coder
...

Now in the Meteor app, add Fairs = new Meteor.Collection("fairs"); into js file to use the data.

From JSON File

Json Lint

Validate the json file with jsonlint.

You need install node.js, then install jsonlint with npm install jsonlint -g. Next check the json file with jsonlint myfile.json.

Import from Json File

Use the following commands to import json objects from a file:

mongoimport -h localhost:3001 -d meteor -c fairs --type json --jsonArray --file demo.json

Or if your mongod is running on localhost, use the following line to import (no need to create the target database and collection before import):

mongoimport -d meteor -c fairs --type json --file first31.json --jsonArray

This is a sample of the imported json file:

[
  { name: "Widget 1",
    desc: "This is Widget 1"
  },
  { name: "Widget 2",
    desc: "This is Widget 2"
  }
]

If the "--jsonArray" option is omitted, the json file have to keep each object in one line, like this:

{ name: "Widget 1", desc: "This is Widget 1" }
{ name: "Widget 2", desc: "This is Widget 2" }

See mongoimport --help for details.



Published

Dec 8, 2014

Last Updated

Dec 8, 2014

Category

Tech

Tags

  • csv 4
  • import 3
  • json 7
  • meteor 47
  • mongodb 24

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor