Home > Uncategorized > Importing .JSON file into your remote mongoDB database with MongoHQ

Importing .JSON file into your remote mongoDB database with MongoHQ

mongohq

MongoHQ is the first DBaaS (Database as a Service) of its kind. It provides free hosting space to your non-commercial mongo powered apps. First register at mongoHQ and create your database. Then they will provide the details for the terminal operations as follows.

mongo widmore.mongohq.com:10010/course -u <user> -p<password>

Here course is my db name. Now Lets import the following grades.json file into the course db.

<br />{ "student" : "Joe", "assignment" : "hw1", "grade" : 90 }<br />{ "student" : "Joe", "assignment" : "hw2", "grade" : 80 }<br />{ "student" : "Joe", "assignment" : "hw3", "grade" : 85 }<br />{ "student" : "Joe", "assignment" : "exam", "grade" : 100 }<br />{ "student" : "Steve", "assignment" : "hw1", "grade" : 80 }<br />{ "student" : "Steve", "assignment" : "hw2", "grade" : 90 }<br />{ "student" : "Steve", "assignment" : "hw3", "grade" : 100 }<br />{ "student" : "Steve", "assignment" : "exam", "grade" : 100 }<br />{ "student" : "Amanda", "assignment" : "hw1", "grade" : 100 }<br />{ "student" : "Amanda", "assignment" : "hw2", "grade" : 90 }<br />{ "student" : "Amanda", "assignment" : "hw3", "grade" : 80 }<br />{ "student" : "Amanda", "assignment" : "exam", "grade" : 100 }<br />{ "student" : "Susan", "assignment" : "hw1", "grade" : 100 }<br />{ "student" : "Susan", "assignment" : "hw2", "grade" : 90 }<br />{ "student" : "Susan", "assignment" : "hw3", "grade" : 85 }<br />{ "student" : "Susan", "assignment" : "exam", "grade" : 80 }<br />

Go to the terminal and execute the following command.

mongoimport -h widmore.mongohq.com --port 10010 -d course -c grades -u supun -p ******** --type json --file /Desktop/grades.json

Here are the list of options which have used above.

Option Description
-h This parameter specifies the host (or server) name.
--port This parameter specifies the port on the host that the MongoDB database is listening on. It is --portinstead of -p to prevent confusion with the password parameter.
-d The name of your database.
-c The name of the collection to import the CSV file into.
-u Since MongoHQ requires authentication, the database username that you specified.
-p The database password that you specified.
--type The format of the file to be imported … generally CSV or JSON.
--file The physical location of the file … generally on your computer.
--headerline This simply tells MongoDB to disregard the first line of the CSV file.

Now lets query this using our nodejs application.

app.js

var MongoClient = require('mongodb').MongoClient;MongoClient.connect('mongodb://supun:********@widmore.mongohq.com:10010/course', function(err, db) {<br /> if(err) throw err;//var query = { 'grade' : 100 };</p><p>db.collection('grades').find().sort([["grade",-1]]).toArray(function(err, docs) {<br /> if(err) throw err;</p><p>console.dir(docs);db.close();<br /> });<br />});

Now execute npm install mongodb

then node app.js

Here is the result.

{ _id: ObjectId("521c44526b39dd11b60564d7"), student: "Joe", assignment: "exam", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564da"), student: "Steve", assignment: "hw3", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564db"), student: "Steve", assignment: "exam", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564dc"), student: "Amanda", assignment: "hw1", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564df"), student: "Amanda", assignment: "exam", grade: 100 }\{ _id: ObjectId("521c44526b39dd11b60564e0"), student: "Susan", assignment: "hw1", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564d4"), student: "Joe", assignment: "hw1", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564d9"), student: "Steve", assignment: "hw2", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564dd"), student: "Amanda", assignment: "hw2", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564e1"), student: "Susan", assignment: "hw2", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564d6"), student: "Joe", assignment: "hw3", grade: 85 }{ _id: ObjectId("521c44526b39dd11b60564e2"), student: "Susan", assignment: "hw3", grade: 85 }{ _id: ObjectId("521c44526b39dd11b60564d5"), student: "Joe", assignment: "hw2", grade: 80 }{ _id: ObjectId("521c44526b39dd11b60564d8"), student: "Steve", assignment: "hw1", grade: 80 }{ _id: ObjectId("521c44526b39dd11b60564de"), student: "Amanda", assignment: "hw3", grade: 80 }{ _id: ObjectId("521c44526b39dd11b60564e3"), student: "Susan", assignment: "exam", grade: 80 }
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: