Source from :http://www.hongkiat.com/blog/webdev-with-mongodb-part1/#mongodb_win
Why Choose MongoDB?
There are a few other schemaless databases you could choose to try other than Mongo. Personally I have only heard great things about the project and it supports a wide range of Operating Systems (Windows, OSX, Linux). Also the core team is still active in pushing code releases in a timely manner.
On the MongoDB website I came across an article comparing MongoDB to CouchDB, which is currently owned by Apache. They both use JSON for storing data and can both power high-volume applications. This may be another scenario where personal opinion has the authority but I feel that Mongo offers more support and documentation for newcomers.
I think MongoDB is probably best used in a situation where you’re testing a new application and seeing how you could structure a database with free-form objects. SQL can be very limiting and I would consider a schemaless database to be even more useful when building mobile apps.
There is even an open source Objective-C Mongo library for iPhone developers and plenty of other resources for Android programmers using Java.
I would argue the reliability of MongoDB and the collective code bases available is one of the strongest reasons to learn this system. There are drivers for nearly any language including C/C++, Python, PHP, Ruby, Perl, .NET, even Node.js.
It’s a powerful solution for web developers who are tired of the limitations of using SQL and want to try something new.
Before we get to installing and testing a Mongo database I’d like to explain a couple of terms which may be confusing later on.
MongoDB can still hold various databases of different names within one installation package, just like MySQL. But inside databases are collections, not tables.
A collection may be considered a table except there are no aligned columns. Each entry (row) can use varying dynamic schemas in key-value pairs.
Inside a collection of Users there may be one entry with First name & Last name. Then another entry with First, Last, and Middle name, along with e-mail address and date of birth. This is the flexible MongoDB system which makes these databases easy to work with.
Now each of these entries or rows inside a collection is called a document. They are not physical documents like .txt or .html, but document-based objects. They are basically JSON data blocks stored in memory-mapped files which behave as separate entries in your collections.
In our collection of Users we may have 500 total document objects for all 500 of our users. And as mentioned before, each document doesn’t need to match other documents with the same fields. The only requirement is a unique ID number which MongoDB will automatically add into each document object.
I can understand this terminology will be confusing at first. It makes a lot of sense once you see everything working in action. If you have a couple minutes try scanning through the Mongo online docs to clarify any fuzzy ideas.
And even if you don’t have the concept down 100% that’s okay! Many people use MySQL on a daily basis with the same vague understanding of how databases work.
Let’s now jump into everything and setup our instance of MongoDB.
Custom MongoDB Setup Process
I want to go over the process for both:
Setting up MongoDB on Mac OS X, and
Setting up MongoDB on Windows
I’ll be using the latest version of OSX Mountain Lion and Windows 7. After the Mongo install we’ll need to configure a local server with WAMP for Windows or MAMP on OS X.
Installing Mongo on Mac OS X
Getting MongoDB onto your Mac will be a lot easier than Windows. Through the Terminal we can run a few commands to pull down the latest version from MacPorts. The online documentation actually has a small tutorial for Mac OS X which features similar information.
To begin let’s check and update to the latest version of MacPorts. In Terminal enter the command:
sudo port selfupdate
You can alternatively add the debug flag for verbose output like so:
sudo port -d selfupdate
The process could take a few minutes to download and install everything.
After this update completes then we need to run just a single line in the Terminal. This will pull down the most recent MongoDB library files and store them with other system dependencies.
port install mongodb
This process could easily take 10-15 minutes even with a quick Internet connection. The install requires a bit of time and could be quicker, but let it go until you get the flashing terminal command open.
Then you should be able to start the server by running:
It should end with the phrase “waiting for connections on port 27017“.
This means we have the database service up and running properly. You can test this by visiting http://localhost:28017/ in your web browser. MongoDB provides a small web interface to view more info about your databases and installation.
The last interesting bit here is to force mongod to open immediately when your computer starts. Hunter Ford has an excellent tutorial on his website which is the basis for my code here. By forcing the mongod process as a startup item you won’t need to keep the terminal window open for development.
First you need to download and move this document to: /Library/LaunchDaemons/org.mongo.mongod.plist.
Then in terminal create a small log file and a directory for the new system data:
sudo touch /var/log/mongodb.log
sudo mkdir /var/lib/mongodb
Finally run these following commands to setup the launcher. You can choose to restart your computer afterwards and see if Mongo works right on reboot.
sudo chown root:wheel /Library/LaunchDaemons/org.mongo.mongod.plist
sudo launchctl load /Library/LaunchDaemons/org.mongo.mongod.plist
sudo launchctl start org.mongo.mongod
Installing Mongo on Windows
I had a very difficult time getting MongoDB to install and run properly without using the Administrator account. This isn’t a requirement with the “Run as Administrator” option is always available.
But if you have the ability, just run this in command prompt and then restart your computer. You should notice the Administrator as a new login selection.
net user administrator /active:yes
If you run into trouble, MongoDB has a great online install guide created specifically for Windows users. To get your copy, visit the downloads page and find your version of Windows. As of the writing this article, the latest stable release is MongoDB 2.2.0: here are the win32 and win64 direct downloads.
We are going to place all these files directly inside a directory C:\mongodb\. So once the download finishes, extract the zip and open the folders until you find /bin/ with a few other files. Select all these and cut/paste into our new C:\mongodb\ directory.
Now still inside this folder alongside \bin\ create a new folder named “log” which will store all the MongoDB system logs. We also need to create two external directories for data storage, C:\data\ and C:\data\db\.
This is the part where using a non-Administrator account may cause trouble. Open up the command prompt and run cd C:\mongodb\bin.
Then we are looking to start the mongod.exe in shell, but after running this you’ll notice the operation will freeze when listening for connections. Well it’s not actually frozen, we are running Mongo directly through the terminal.
This will get annoying to do every time, so let’s run a command configuring Mongo to start automatically as a Windows Service.
> echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg
This first command will create a log file and configuration for the service. Not required, but good practice as a database administrator.
Now run these two lines of code in terminal to create the service and get it started.
> C:\mongodb\bin\mongod.exe –config C:\mongodb\mongod.cfg –install
> net start MongoDB
If you get no errors then the process is all done!
Check if the service is active by opening the Run menu (Windows + R) and typing services.msc.
This brings up an active list of services and if you scroll down you should find Mongo DB with the status “active” and the startup type “automatic”.
As on the Mac install you can access the Mongo shell terminal right from the command prompt. Change directories to C:\mongodb\bin\ and type mongo, then hit Enter.
You should gain access right into the current MongoDB server. Now you can run Mongo shell commands to create databases, collections, store new data, or edit old data entries.
Run the following line to show all current databases on the server:
> show dbs