NoSQL Zone is brought to you in partnership with:

Adrian is a freelance consultant Java Enterprise developer with nearly 20 years experience of software development across a wide range of companies, sectors and projects. In recent years he has formed his own company and dabbled with mobile app development alongside his consultancy work. Adrian is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

MongoDB in 3 Easy Steps!

08.06.2013
| 3501 views |
  • submit to reddit

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
print("Starting MongoDB Library Database Build Script")

print("Creating Libary Database")
var libraryDB = db.getSiblingDB("library")

print("Creating Book Collection")
libraryDB.createCollection("book")

print("Creating index on 'type' field")
libraryDB.book.ensureIndex({type:1})

print("Populating Book Data")
libraryDB.book.insert({name:"The Lord Of The Rings",author:"J.R.R. Tolkien",type:"Fantasy"})
libraryDB.book.insert({name:"The Silmarillion",author:"JRR Tolkien",type:"Fantasy"})
libraryDB.book.insert({name:"Dune",author:"Frank Herbert",type:"Sci-Fi"})

print("Finished MongoDB Library Database Build Script")
  • 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

Published at DZone with permission of Adrian Milne, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)