Welcome to our series of Strata Gems. We’ll be publishing a new one each day all the way through to December 24.
Document store databases such as MongoDB and CouchDB offer a scalable way to store semi-structured data. If you’re looking for an easy way to get started, without the pain of installing either a single instance or a cluster of database servers, MongoDB is a good choice.
MongoDB lets you store and query data expressed as JSON documents. It offers conventional indexing, replication and lets you perform map-reduce jobs on database contents. You can get started from the comfort of your browser by heading over to the MongoDB web site and using the online “Try It Out” interface.
The MongoDB shell is an interactive JavaScript interpreter.
> foo = 32; > print(foo); 32 >
The MongoDB “Try It Out” browser-based shell
Here are a couple of sample JSON documents we’ll store in Mongo.
> var fred = {name: "Fred Flintstone", age: 32}; { "name" : "Fred Flintsone", "age" : 32 } > var barney = {name: "Barney Rubble", age: 31}; { "name" : "Barney Rubble", "age" : 31 }
Now we’ll save them into a database called “characters” – created automatically when referenced – and query for all the documents in the database.
> db.characters.save(fred); "ok" > db.characters.save(barney); "ok" > db.characters.find(); [ { "_id" : { "$oid" : "4cf69bc3cc9374271b0137c0" }, "name" : "Fred Flintstone", "age" : 32 }, { "_id" : { "$oid" : "4cf69bc7cc9374271b0137c1" }, "name" : "Barney Rubble", "age" : 31 } ]
Let’s run a few more complex queries, finding the characters who have age equal to 31, and who have age greater than 30.
> db.characters.find({age: 31}); [ { "_id" : { "$oid" : "4cf69bc7cc9374271b0137c1" }, "name" : "Barney Rubble", "age" : 31 } ] > db.characters.find({age: {'$gt': 30}}); [ { "_id" : { "$oid" : "4cf69bc3cc9374271b0137c0" }, "name" : "Fred Flintstone", "age" : 32 }, { "_id" : { "$oid" : "4cf69bc7cc9374271b0137c1" }, "name" : "Barney Rubble", "age" : 31 } ]
This is just a small taste of what you can do with MongoDB. To dive further, enter tutorial
into the “Try It Out” interface and work through the steps, then download MongoDB and try it out on your own machine.