MongoDB

"hu mongo us"

Document-oriented database

No NoSQL for you!

Come back next year:

Features

Brownie Points

Scale and trade offs

BSON/JSON

JSON data types

BSON extra types

Hello schemaless

Just like discovering dynamic languages for the 1st time (plus it maps smoothly to dynamic languages).

Not exactly schema less (more of dynamic one):

Collections are created as needed (on write).

Documents

Example document

{
  _id: "September 19th, 2012",
  talks: [
    {
      title:  "Meta-Moose",
      lecturer: "Ynon Perek",
      description: "Using the meta object, dyna ..."
      slides: "http://www.slideshare.net/YnonPerek/metamoose-14339965"
    },
    {
      title: "Project Euler",
      lecturer: "Shlomi Fish",
    }, { ... }
  ]
}

_id

mongo shell

Basically a JS shell. Let's play with it a bit.

Querying

db.products.find({'type': 'case'})

Find all documents in collection products where type is "case" (even if type is an array).

Advanced

Many query operators, like

Updates

Some of the update operators (in addition to save() ):

Programming Languages

Has official support for many languages:

C, C++, C# / .NET, Erlang, Haskell, Java, JavaScript, Lisp, node.JS, Perl, PHP, Python, Ruby, Scala

Plus few unofficial ones.

For Perl, there's MongoDB at CPAN

Perl example

use MongoDB;

my $conn = MongoDB::Connection->new;
my $catalog_db = $conn->pcat;  # pcat db

my $products = $catalog_db->products;  # products collection

my $services = $products->find({type => "service"}); # we get a cursor

while (my $service = $services->next) {
    print $service->{'_id'} . ', ' . $service->{'name'} . "\n";
}

Insert

$products->insert({
    name => "Dummy box", type => ["service", "dummy"]
});

# or

$catalog_db->products->insert({
    name => "Dummy box", type => ["service", "dummy"]
});

Update

# For type service increment price by 20, set the raised flag

$products->update(
    {type => "service"},
    {'$inc' => {monthly_price => 20},  {'$set' => {raised => 1}} }
);
# with boolean. To get booleans set $MongoDB::BSON::use_boolean to 1

use boolean;
$products->update(
    {type => "service"},
    {'$inc' => {monthly_price => 20},  {'$set' => {raised =>true}} }
);

Capped collections

Let's talk a bit about capped collections and tailabile cursors.

my $cursor = $coll->find->tailable(1);
for (;;) {
    if (defined(my $doc = $cursor->next)) {
        say $doc;
    }
    else {
        sleep 1;
    }
}

Something from the trenches

Resources

Thank you