Importing JSON into a Meteor Mongo Database

Meteor is a great way to develop fast real-time appliations using JavaScript on both the client and server side. When you are developing these applications it's easy to create new collections of information by adding items doing the following script either in the client or server:

Collection.insert({key:value});

But this is obviously not the most effective way to add lots data!

MongoDB has a tool called mongoimport which you can use to import data straight into a MongoDB instance itself, but that tool doesn't come with Meteor. What to do???

The answer is actually rather simple, all you have to do is install MongoDB but you don't need to run an instance of it, as you are already doing that with your Meteor installation.
Once you have it installed check in the <MongoDB Install Path>/bin and you should see the mongoimport executable. Now, to import some json you need to tell it the host and database of the Meteor instance of MongoBD:

mongoimport -h localhost:3001 --db meteor --collection tweets --type json --file exampletweets.json --jsonArray

That is a long command so let's break it down:
mongoimport is obvisouly the command.
-h is the host and port
--db is the database, in this case it's meteor
--collection is the collection we are going to be importing into
--type is the format of the file we will be importing, in this case a json file of tweets
--jsonArray MongoDB has a strange idea of what a json format is, if you omit this it will want a file in the format:

{key:value}
{key:value}
{key:value}

Which I am sure you can tell is not JSON formatted. So if you want proper JSON format like:

[
{key:value},
{key:value},
{key:value}
]

You need to add the --jsonArray so that MongoDB accepts it as plain JSON.

 

On a side note here, if you want to find out what host and port your Meteor instance of MongoDB is running on you just have to do:

meteor mongo

And you should get a response like:

MongoDB shell version: 2.6.7
connecting to: 127.0.0.1:3001/meteor
meteor:PRIMARY>

Hope that helps!

 

Quick tip: How to run a web server using the current path

A lot of times you want to run a simple web server from the current path. Whether it be to see how javascript behaves or if you have linked things right in an html file. A quick tip is to install an npm package called http-server (the clue is in the name!) 

Installation is pretty simple, by the usual npm route:

npm install http-server -g

And then you just have to go to the folder you want to serve files from and do:

node bin/http-server

or the following just worked for me:

http-server

Hope that helps!

CMD: One year on

Hello Internet

I know it has become a bit of a tradition for me to announce things on the 1st of April. This year it will be slightly different. It will be a good news post. 

I started CMD one year ago today and have to say that it has been one of the most fun years I have had. 

On a personal level, I got married, bought a house and had an awesome honeymoon in Cancun. 

On a professional level, CMD has thrived in the year, we have manged to pay wages, sponsor conferences and even had some awesome swag produced. 

We have had some great clients, we also some interesting products in development and we will be attending  dev.objective() in May which I hope to meet up with faces old and new. 

I am totally looking forward to the next year! 

Cheers!

 

 

Say Hello To Lucee

Say hello to

More info over at the CMD blog

... 0 ...

So soon! You can almost hear it.

Are you ready?