Welcome to Learn Mongo

in Announcements

Hello! Thanks for stopping by.

This site is intened to serve a resource for those learning (or wanting to learn more about) MongoDB.

We are not offically accociated with MongoDB but we sure think it’s cool.

Here you will find ..

  • Short blog posts on MongoDB features & tips (there are already a few below.)
  • Tutorials (a couple coming soon.)
  • Collection of videos/screencasts from around the web  in our videos section.
  • All, with a dash or two of humor.
  • Free.

So, checkout what we have so far and let us know what you think by commenting or e-mailing us at info@learnmongo.com

0 Comments

Backup Solutions: Without Backing Up

in Administration

To do a proper backup of MongoDB requries you shutdown the server instance, run a backup then turn the server back on. This might be just fine for some use cases but more then likely you don’t want to have to turn off your server (if only to keep your uptime: 3790344373243 seconds in your mongodb stats.)

Why Do You Need to Shutdown?

Trying to make a copy of your MongoDB files for a backup while the server is running will often result in corrupted files.

In MongoDB 1.3.1 (and up) you can use the fsync and lock command (which flushes writes, locks the database then backs up the datafiles.) However this still requires you to lock you database for writing for an unknown period of time.

Backup: Now Without Backup!

We can get around all these issues however with two different options: mongodump or slave backups.

mongodump

This method works much like mysqldump, it simply takes all the data for a certain collection or your entire database of collections and outputs it into text files.

A copy of mongodump comes with the download of MongoDB and runs like so (assuming you are on the localhost):

$ ./mongodump

If you dumping a external host (like dump.tuck.com) just add …

$ ./mongodump --host dump.truck.com

The datafiles will be dumped to a directory call dump in your current directory.

To back up just one collection:

$ ./mongodump --db meals --collection dinners

You can then use mongorestore to restore you database from backup. You can run a dump while the database is running without fear of any corruption.

Important Note: The official MongoDB DOCs give this warning about mongorestore:

These tool just work with the raw data (the documents in the collection); they do not save, or load, the metadata like the defined indexes or (capped) collection properties. You will need to (re)create those yourself in a separate step, before loading that data.

Of course, if you don’t use custom indexes and such you won’t run into a problem.

Slave Backup

Another option is to use what is called “slave backups” … basically you setup a slave MongoDB server, then use that to do your backups (shut it down, backup, start it again) … never touching your production server at all.

A MongoDB server in slave mode will constantly poll your master server to get the latest copy of each collection and create a duplicate on your slave.

This also means you have a copy (or twin) of your production database always ready to go in the unfortunate event that your production server goes down.

Start Me Up

To create a slave all you need to do is download MongoDB to another machine and install it or even run MongoDB twice on differant ports on the same machine!

You can use this to keep an up-to-date “backup” always ready for you without needing to run any scheduled backups or taking down you server.

$ ./mongod --slave --source mongo.example.com

Or, on the same machine, just choose a new port, like 123456

$ ./mongod --port 123456 --slave --source mongo.example.com:28017

Now, login  to you slave server’s MongoDB shell and shut it down …

> db.shutdownServer()

Tun your backups and start you slave back … up, again.

Twins Are So Cute

There is no reason your slave sever needs to be as robust as your production server (all you are only using it for backup purposes) … it need only have adequate disk space (so you might want a 64 bit machine if you can.)

So, you can have a “twin” of your production server that looks nothing like your production server (spec wise) kinda like that great Arnold & Danny movie … and your slave might even be a better actor then your master.

0 Comments

Good to the Last $slice

in Querying

What if we want to query just a subset of elements in an array or sub-document?

Well let me tell tell you how MongoDB will slice, will dice, will …

Okay let’s just focus on the $slice operator (you will need at least MongoDB 1.5.1) …

Our Pizza Dinner

Suppose you have a pizza document like this …

{
 "_id" : ObjectId("497ce96f395f2f052a494fd4"),
 "food" : "pizza",
 "toppings" : { "pepperoni", "artichoke hearts" },
 "delivered" : "8/12/2009 06:46 PM",
 "notes" : "Half pepperoni and artichoke hearts. Half cheese only.",
 "pieces" : [
   { "piece" : 1 },
   { "piece" : 2 },
   { "piece" : 3 },
   { "piece" : 4 },
   { "piece" : 5 },
   { "piece" : 6 }
 ]
}

Yum. Lets $slice up our Pizza!

The document above describes out pizza: it’s half pepperoni and artichoke hearts (trust me it’s good) and half plain cheese.

There is an array of pieces but we don’t know which piece has which topping. All we know is the “second” half of the pizza has the yummy toppings.

How exactly can you bring back only the last 3 pieces without getting the all the pieces in the pizza array? Simple.

(BTW, I know there are other ways to do this, this is just a fun example: a production example would be comments in a blog post.)

> db.meals.find({ food:"pizza" }), { pieces: { $slice: -3 }});

You will then only get back the last 3 pieces in the pizza array (pieces 4,5,6) delish…

To get the next 3, we can skip the last 3 then get 3 more …

> db.meals.find({ food:"pizza" }), { pieces: {$slice: [-3, 3] }});

You could also pass [-3, 1] or [-3, 2] … etc. to get 1 or 2 or however many more pieces you want.

Are You Full Yet?

If I made you hungry … try the pepperoni and artichoke hearts … you’ll love it.

1 Comment

Being Part of the $in Crowd

in Querying

Everyone wants to be part of the in-crowd, but not everyone can …

Say you have a couple people represented in Documents like this:

{ "_id" : ObjectId("497ce96f395f2f052a494fd4"),
  person : "Kim", crowd : "cool" }
{ "_id" : ObjectId("497ce96f395f2f052a494fd5"),
  person : "Steve", crowd : "losers" }
{ "_id" : ObjectId("497ce96f395f2f052a494fd6"),
  person : "Ji Sung", crowd : "coolest" }
{ "_id" : ObjectId("497ce96f395f2f052a494fd7"),
  person : "Nigel", crowd : "cool" }
{ "_id" : ObjectId("497ce96f395f2f052a494fd7"),
  person : "Bono" }
{ "_id" : ObjectId("497ce96f395f2f052a494fd8"),
  person : "Robert", crowd : "okay" }
{ "_id" : ObjectId("497ce96f395f2f052a494fd9"),
  person : "William", crowd : "losers" }

Those Poor Losers

Now we only want to get back people that are in the … the in-crowd.

Our in-crowd consists of the “coolest”, “cool”, and “okay” people (we are being nice) but not the “losers” …

Here is how we do it, with the $in operator.

> db.people.find({ crowd : { $in: ["coolest","cool","okay"] }});

See, wasn’t that easy?

15 Year Later: When the Losers are Rich

Now, we all know that a lot of the so-called “losers” in High School turned out to be us, the now successful computer people!

Say we want to do the query the other way around … we can use the Nine Inch Nails operator also known as the $nin operator:

> db.people.find({ crowd : { $nin: ["coolest","cool"] }});

Now we will only get back the “losers” and the “okay” people (we are being nice) … so the in-crowd can hit them up for a job.

The Bono Factor

None of those “cool” kids in High School are hanging out with Bono like Bill Gates!

Note: Bono would have come back in the above query since he isn’t in any crowd! See above.

0 Comments