As you have seen recently, i’m not writing so much here, because i’m working on new technologies like NodeJS and MongoDB. Fascinated by this next-gen “AMP”, i’m trying to write my first scripts trying to make such a basic job: get some data from somewhere, save them into the DB then next, and finally, get all the items that are in our “table” (i’ll explain you later why i’m writing it like this).
First of all, i have to explain you something before we can start. MongoDB, as you may know, is a next generation NO-SQL in-mind to store data in it. But, just like SQL Databases, they do not use “tables” as we do not have to declare table columns or something like that. We have collections defined by models. Something like virtual “tables” where:
- Collection = list of “tables”
- Models = column of the tables (with flexible data type: integers, strings, arrays or objects)
So, when you’ll catch this knowledge, you’ll be ready to start this simple tutorial. What we will need to accomplish this are:
- NWM (made by me, a simple “WAMP-like”, ready-to-use with NodeJS and MongoDB)
- MongoVue (to inspect the database, just like PhpMyAdmin, feel free to use the one you prefer, optional)
- MongoDB instance running (run your “start MongoDB” script inside NWM, usually located at Start -> All Programs -> NWM -> Start MongoDB).
Obviously, i’m not going to explain how to install them, basically you have to download them, install and you’re done. For MongoDB will be used the “test” database, automatically created for default setups.
Open your Command Prompt (until now, it will be called CMD) and write these command
what we have done here, was adding a user for the connection (actually not really required, as we’re in a testing environment) and initializing the database.
Just like PHP, also NodeJS (let’s think about it, like an Apache+PHP) does not have a native library to connect to MongoDB, so we have to install it by ourself. So open your CMD and type this command
npm install mongoose
and wait until it finishes.
Yes, it’s not just like Apache that you have a “www” directory where you put your scripts and then point to your “localhost” and you can see them. In NodeJS you have to run your script by typing this in your CMD
So what what we have to do now is copying this script
and save it to our preferred location (the Desktop will be fine) and call it “test.js“. Then finally, to test it, just type
and point your browser at http://localhost/ so you can view your data.
You made it! You’re runnig the “Hello MongoDB” script and you’re pushing data into the database. After this, you’re getting ALL the data inside the database.
So, what will MongoVUE will be need for? Well, if you’re curious to know how the data is inside (let’s think about PHPMyAdmin ) and you want to view/edit/delete data into, you can do that. I’m NOT going to explain how to use it, think about this as a reference. You can find more Admin UIs for MongoDB at http://www.mongodb.org/display/DOCS/Admin+UIs
Comment below if you find any mistake or you have any suggestion regarding this article.
So, thanks for reading, i hope you’ll enjoy this.