Mongo Seattle 2011

in Announcements, MongoSeattle

I’m happy to (sorta) announce I (Justin) will be giving a talk at Mongo Seattle 2011 on Dec 1st in downtown Seattle.

According my contacts at 10Gen the registration is going gang-busters and there are going to be some really great sessions to attend including one from our buddy Damon Cortesi at Simply Measured as well as a bunch of 10Gen engineers (the people that make MongoDB) plus MongoLab, Geek.net, VMWare and WordSquared (a super fun game!)

So, what are you waiting for?

Go ahead a register here  … and if you send me a DM or @Reply on Twitter (@learnmongo) I might just give you a pretty decent discount code!

 

0 Comments

Compacting MongoDB Collections

in Administration

A while back we wrote a post explaining how to compact MongoDB data files, that example shows how to use some server side javascript and a cron job to automatically compact the data files on a schedule … however this isn’t always ideal as it will compact all the collections in a database at one time.

If you have a very large Collection you didn’t want to run the operation on …or a number of Collections in your database you didn’t wish to compact you were a bit out of luck.

Also, using repairDatabase() requires the disk space for the current database plus the repaired copy (i.e. double the disk space.)

Enter MongoDB 2.0 … in 2.0+ you can now use the simple compact command to target the compact to a single collection.

Running compact has three major performance benefits:

  • Compacts collection (less disk space.)
  • Defragments a collection (data pages are aligned better.)
  • Rebuilds and compacts the collection’s indexes (less RAM needed, and better perf.)

If  you have a lot of read/write/delete operations going on in your Collection this could possibly have a fairly noticeable performance impact.

There are two ways to run the compact command:

> db.yourCollection.runCommand("compact");
> db.runCommand({ compact : 'yourCollection' });

Now, that said there are two big downsides to using compact …

  • The compact command blocks operations on the collection until it’s done compacting (so it’s best to run this off hours during scheduled maintenance.)
  • It’s typically slower than repairDatabase in its actual operational time.

Those aside, as part of routine maintenance compact is a really helpful new feature and might justify an upgrade to 2.0 all on it’s own!

0 Comments

Quick Tip: How to $size up a MongoDB Array

in Querying, Quick Tip

Since MongoDB will allow you to store more than just string and int values but also things like arrays … from time to time you might need to know how many items are in an array in your document.

For example, say you have a hosting business and to make a good profit you need to sell at least three upgrades to each hosting client.

To make sure you market to the correct people you want to find all the clients with two upgrades (and get them to sign up for just one more.)

Your general document structure looks something like this:

{
 customer_name : "Sam Taylor",
 email : "sam@widgetxyz.com",
 upgrades: ["ssl", "diskspace 3", "RAM 5"]
}
{
 customer_name : "Kyle Lopez",
 email : "klopez@buymybikes.com",
 upgrades: ["diskspace 3", "RAM 5"]
}

To get all the clients that have two upgrades we’ll use the $size operator:

> db.clients.find( { upgrades : { $size: 2 } } );

Now you’ll get back all the documents that have two upgrades in the “upgrades” array (or in this example, the document for “Kyle Lopez”.)

What about a range?

Unfortunately the $size operator doesn’t support ranges (like getting all documents with 2 to 4 array values.) To do that you’d need to create your own separate field to keep track of the count and query on that field.

0 Comments

Quick Tip: Copy DBs Between Servers

in Administration, Quick Tip

Inspired by this Stackoverflow question I thought it would be worth while to post this quick tip …

If you ever need to copy (or “clone”) a MongoDB databse between two MongoDB servers its as easy as running the copyDatabase() command.

First, change into your admin db …

Then, run copyDatabase() supplying the needed information, the “from_db” (the database to copy) the “to_db” and the host name of the database you are copying …

> use admin;
> db.copyDatabase(…);

You’ll need to run the copyDatabase() command on the sever you want to “copy” the database to, aka the “destantion” server …

> db.copyDatabase(from_db, to_db, from_host);

If you are running MongoDB with accounts/password via –auth you’ll need to add that info on there too …

> db.copyDatabase(from_db, to_db, from_host, username, password);

A simple example for a database named “weyoun” on a host called “delta.quad.com” would be like so …

> db.copyDatabase('weyoun', 'weyoun', 'delta.quad.com');

There ya go, you should be good to go copying your MongoDB database!

 

1 Comment

Sorry It Has Been So Quiet …

in Announcements

Dear Readers,

Sorry it’s been so quiet lately … thing is LearnMongo is moving!

Well not moving sites or anything … but I’m moving … to the sunny North West (at least that’s what my new boss keeps telling me?) …

Seattle Bound (wait that sounds wrong)

Once all the moving stuff is taken care of LearnMongo will be back to regular posts (staring in March.)

I’m sure I’ll have plenty of coffee to fuel some awesome Mongoness.

Thanks for your support so far, and I’m looking forward to lots more Mongo in 2011.

~ Justin

3 Comments