#
#  Copyright 2009-2013 10gen, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#


# PODNAME: MongoDB::Examples
# ABSTRACT: Some examples of MongoDB syntax

__END__

=pod

=head1 NAME

MongoDB::Examples - Some examples of MongoDB syntax

=head1 VERSION

version 0.702.2

=head1 MAPPING SQL TO MONGODB

For developers familiar with SQL, the following chart should help you see how
many common SQL queries could be expressed in MongoDB.

These are Perl-specific examples of translating SQL queries to MongoDB's query
language.  To see the JavaScript (or other languages') mappings, see
L<http://dochub.mongodb.org/core/sqlToMongo>.

In the following examples, C<$db> is a L<MongoDB::Database> object which was
retrieved by using C<get_database>. See L<MongoDB::MongoClient> for more.

=over

=item C<CREATE TABLE USERS (a Number, b Number)>

    Implicit, can be done explicitly.

=item C<INSERT INTO USERS VALUES(1,1)>

    $db->get_collection( 'users' )->insert( { a => 1, b => 1 } );

=item C<SELECT a,b FROM users>

    $db->get_collection( 'users')->find( { }, { a => 1, b => 1 } );

=item C<SELECT * FROM users>

    $db->get_collection( 'users' )->find;

=item C<SELECT * FROM users WHERE age=33>

    $db->get_collection( 'users' )->find( { age => 33 } )

=item C<SELECT a,b FROM users WHERE age=33>

    $db->get_collection( 'users' )->find( { age => 33 }, { a => 1, b => 1 } );

=item C<SELECT * FROM users WHERE age=33 ORDER BY name>

    $db->get_collection( 'users' )->find( { age => 33 } )->sort( { name => 1 } );

=item C<<SELECT * FROM users WHERE age>33>>

    $db->get_collection( 'users' )->find( { age => { '$gt' => 33 } } );

=item C<<SELECT * FROM users WHERE age<33>>

    $db->get_collection( 'users' )->find( { age => { '$lt' => 33 } } );

=item C<SELECT * FROM users WHERE name LIKE "%Joe%">

    $db->get_collection( 'users' )->find( { name => qr/Joe/ } );

=item C<SELECT * FROM users WHERE name LIKE "Joe%">

    $db->get_collection( 'users' )->find( {name => qr/^Joe/ } );

=item C<<SELECT * FROM users WHERE age>33 AND age<=40>>

    $db->get_collection( 'users' )->find( { age => { '$gt' => 33, '$lte' => 40 } } );

=item C<SELECT * FROM users ORDER BY name DESC>

    $db->get_collection( 'users' )->find->sort( { name => -1 } );

=item C<CREATE INDEX myindexname ON users(name)>

    $db->get_collection( 'users' )->ensure_index( { name => 1 } );

=item C<CREATE INDEX myindexname ON users(name,ts DESC)>

    $db->get_collection( 'users' )->ensure_index( Tie::IxHash->new( name => 1, ts => -1 ) );

In this example, we must use L<Tie::IxHash> to preserve the ordering of the arguments to 
C<ensureIndex>.

=item C<SELECT * FROM users WHERE a=1 and b='q'>

    $db->get_collection( 'users' )->find( {a => 1, b => "q" } );

=item C<SELECT * FROM users LIMIT 10 SKIP 20>

    $db->get_collection( 'users' )->find->limit(10)->skip(20);

=item C<SELECT * FROM users WHERE a=1 or b=2>

    $db->get_collection( 'users' )->find( { '$or' => [ {a => 1 }, { b => 2 } ] } );

=item C<SELECT * FROM users LIMIT 1>

    $db->get_collection( 'users' )->find->limit(1);

=item C<EXPLAIN SELECT * FROM users WHERE z=3>

    $db->get_collection( 'users' )->find( { z => 3 } )->explain;

=item C<SELECT DISTINCT last_name FROM users>

    $db->run_command( { distinct => "users", key => "last_name" } );

=item C<SELECT COUNT(*y) FROM users>

    $db->get_collection( 'users' )->count;

=item C<<SELECT COUNT(*y) FROM users where age > 30>>

    $db->get_collection( 'users' )->find( { "age" => { '$gt' => 30 } } )->count;

=item C<SELECT COUNT(age) from users>

    $db->get_collection( 'users' )->find( { age => { '$exists' => 1 } } )->count;

=item C<UPDATE users SET a=1 WHERE b='q'>

    $db->get_collection( 'users' )->update( { b => "q" }, { '$set' => { a => 1 } } );

=item C<UPDATE users SET a=a+2 WHERE b='q'>

    $db->get_collection( 'users' )->update( { b => "q" }, { '$inc' => { a => 2 } } );

=item C<DELETE FROM users WHERE z="abc">

    $db->get_database( 'users' )->remove( { z => "abc" } );

=back

=head1 DATABASE COMMANDS

If you do something in the MongoDB shell and you would like to translate it to
Perl, the best way is to run the function in the shell without parentheses, which
will print the source.  You can then generally translate the source into Perl
fairly easily.

For example, suppose we want to use C<db.foo.validate> in Perl.  We could
run:

    > db.foo.validate
    function (full) {
        var cmd = {validate:this.getName()};
        if (typeof full == "object") {
            Object.extend(cmd, full);
        } else {
            cmd.full = full;
        }
        var res = this._db.runCommand(cmd);
        if (typeof res.valid == "undefined") {
            res.valid = false;
            var raw = res.result || res.raw;
            if (raw) {
                var str = "-" + tojson(raw);
                res.valid = !(str.match(/exception/) || str.match(/corrupt/));
                var p = /lastExtentSize:(\d+)/;
                var r = p.exec(str);
                if (r) {
                    res.lastExtentSize = Number(r[1]);
                }
            }
        }
        return res;
    }

Thus, we can translate the important parts into Perl:

    $db->run_command( { validate => "foo" } );

=head2 Distinct

The distinct command returns all values for a given key in a collection.  For
example, suppose we had a collection with the following documents (C<_id> value
ignored):

    { 'name' => 'a', code => 1 }
    { 'name' => 'b', code => 1 }
    { 'name' => 'c', code => 2 }
    { 'name' => 'd', code => "3" }

If we wanted to see all of values in the "code" field, we could run:

    my $result = $db->run_command([
       "distinct" => "collection_name",
       "key"      => "code",
       "query"    => { }
    ]);

Notice that the arguments are in an array, to ensure that their order is
preserved.  You could also use a L<Tie::IxHash>.

C<query> is an optional argument, which can be used to only run C<distinct> on
specific documents.  It takes a hash (or L<Tie::IxHash> or array) in the same
form as L<MongoDB::Collection/"find($query)">.

Running C<distinct> on the above collection would give you:

    {
        'ok' => '1',
        'values' => [
                      1,
                      2,
                      "3"
                    ]
    };

=head2 Find-and-modify

The find-and-modify command is similar to update (or remove), but it will return
the modified document.  It can be useful for implementing queues or locks.

For example, suppose we had a list of things to do, and we wanted to remove the
highest-priority item for processing.  We could do a L<MongoDB::Collection/find>
and then a L<MongoDB::Collection/remove>, but that wouldn't be atomic (a write
could occur between the query and the remove).  Instead, we can use find and
modify.

    my $next_task = $db->run_command({
        findAndModify => "todo",
        sort => {priority => -1},
        remove => 1
    });

This will atomically find and pop the next-highest-priority task.

See L<http://www.mongodb.org/display/DOCS/findAndModify+Command> for more
details on find-and-modify.

=head2 Group

The group command is similar to "GROUP BY" in SQL.  You can use the
L<MongoDB::Database/run_command> method to perform group-bys with MongoDB.

For example, suppose we have a number of local businesses stored in a "business"
collection.  If we wanted to find the number of coffeeshops in each neighborhood, we
could do:

    my $reduce = <<REDUCE;
    function(doc, prev) {
        for (var t in doc.tags) {
            if (doc.tags[t] == "coffeeshop") {
                prev["num coffeeshops"]++;
                break;
            }
        }
    }
    REDUCE

    my $result = $db->run_command({group => {
        'ns' => "business",
        'key' => {"neighborhood" => 1},
        'initial' => {"num coffeeshops" => 0},
        '$reduce' => MongoDB::Code->new(code => $reduce)

This would return something like:

    {
          'ok' => '1',
          'keys' => 4,
          'count' => '487', # total number of documents
          'retval' => [
              {
                          'neighborhood' => 'Soho',
                          'num coffeeshops' => '23'
              },
              {
                          'neighborhood' => 'Chinatown',
                          'num coffeeshops' => '14'
              },
              {
                          'neighborhood' => 'Upper East Side',
                          'num coffeeshops' => '10'
              },
              {
                          'neighborhood' => 'East Village',
                          'num coffeeshops' => '87'
              }
            ]
    };

Thus, there are 23 coffeeshops in Soho, 14 in Chinatown, and so on.

See L<http://www.mongodb.org/display/DOCS/Aggregation> for more details on
grouping.

=head2 MapReduce

MapReduce is a powerful aggregation tool.  (For traditional queries, you should
use C<MongoDB::Collection::query>.)

This example counts the number of occurences of each tag in a collection.  Each
document contains a "tags" array that contains zero or more strings.

    my $map = <<MAP;
    function() {
        this.tags.forEach(function(tag) {
            emit(tag, {count : 1});
        });
    }
    MAP

    my $reduce = <<REDUCE;
    function(prev, current) {
        result = {count : 0};
        current.forEach(function(item) {
            result.count += item.count;
        });
        return result;
    }
    REDUCE

    my $cmd = Tie::IxHash->new("mapreduce" => "foo",
        "map" => $map,
        "reduce" => $reduce);

    my $result = $db->run_command($cmd);

See the MongoDB documentation on MapReduce for more information
(L<http://dochub.mongodb.org/core/mapreduce>).

=head1 QUERYING

=head2 Nested Fields

MongoDB allows you to store deeply nested structures and then query for fields
within them using I<dot-notation>.  For example, suppose we have a users
collection with documents that look like:

    {
        "userId" => 12345,
        "address" => {
            "street" => "123 Main St",
            "city" => "Springfield",
            "state" => "MN",
            "zip" => "43213"
        }
    }

If we want to query for all users from Springfield, we can do:

    my $cursor = $users->find({"address.city" => "Springfield"});

This will search documents for an "address" field that is a subdocument and a
"city" field within the subdocument.

=head1 UPDATING

=head2 Positional Operator

In MongoDB 1.3.4 and later, you can use positional operator, C<$>, to update
elements of an array.  For instance, suppose you have an array of user
information and you want to update a user's name.

A sample document in JavaScript:

    {
        "users" : [
            {
                "name" : "bill",
                "age" : 60
            },
            {
                "name" : "fred",
                "age" : 29
            },
        ]
    }

The update:

    $coll->update({"users.name" => "fred"}, {'users.$.name' => "george"});

This will update the array so that the element containing C<"name" =E<gt> "fred">
now has C<"name" =E<gt> "george">.

=head1 AUTHORS

=over 4

=item *

Florian Ragwitz <rafl@debian.org>

=item *

Kristina Chodorow <kristina@mongodb.org>

=item *

Mike Friedman <friedo@mongodb.com>

=back

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by MongoDB, Inc..

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut
