If  you’re entirely new to MongoDB – this is just a very gentle introduction that gets you up and running with a sample database in just a few easy steps.

It’s really easy to get started. The steps are:

1. Install MongoDB
2. Create a Database
3. Query the Database

1. Install MongoDB

  • The mongoDB site has simple, clear instructions for downloading and installing. Just click on the link below and follow the instructions for your particular operating system (much clearer that trying to explain them again here!):

http://docs.mongodb.org/manual/installation/

  • (Make sure you have the have started the ‘mongod‘ server process as explained in these instructions – you should have a mongod server up and running before moving on to Step 2 – like in this screenshot below)

 

2. Create A Database

  • Now we have a server up and running – the next step is to create a database and pop some data in. For our example we have a very simple Library database, with a single ‘book’ table (or ‘collection’ in mongo-speak).
  • Instead of running SQL against mongo – you use Javascript – an example build file is included below (you could enter the commands in the interactive console window – shown later – but for speed and ease of use – you can also run them all together from a script file – which is the approach we use here)
  • Copy the javascript code from the GIST below, and save it to a file called ‘library_build_script.js’ build script on your machine

 

  • Open a new console window, navigate to the ‘bin’ folder in the extracted mongo installation files,  and use the mongo command to load the script – just type:
mongo SCRIPT_LOCATION/library_build_script.js'

  • (Note: if you set the PATH up on your machine, you can of course run the mongo executables from anywhere)
  • this will run the build script to create the database, collection and indexes, and prepopulate with some test data.
  • Note – we are using getSiblingDB() to create and work with a sibling database. This is slightly more complex than the default, but if you create multiple databases, this helps organise them better (as you build databases in a tree like structure). It keeps it easier to manage/view multiple databases on the same server. Have a play around with different options when you get up and running to see what works best for you.

3. Query the Data

  • It’s as simple as that to have a populated database up and running on your machine – we can now use the interactive mongo command line tool to query the data
  • Start up the interactive mongo console. Still in the ‘bin’ folder, type
mongo

 

  • Find All Books Query – list all books in the database
db.getSiblingDB("library").book.find()

 

 

  • Find All Books With Type ‘Fantasy’ Only
db.getSiblingDB("library").book.find({'type':'Fantasy'})

 

As you can see – it’s really easy to get up and running in just a few minutes. Of course there’s much more to cover – but this should be a good starting point to start playing.

I’ll hopefully be expanding on the Library database in some future posts – exploring more advanced features and functions.

Addendum: Using A GUI Client

You can also use a GUI client to explore your local MongoDB database if you prefer that to using the command line.

Check out UMongo – you can download it for free from the website here:

http://edgytech.com/umongo/

Just follow the installation instructions for your OS, start it up and create a connection to your local mongod server.

You can then interact with your local mongo instance using the tools in UMongo

 

(Visited 427 times, 1 visits today)
MongoDB in 3 Easy Steps!
Tagged on:
InstagramPlease check your feed, the data was entered incorrectly.