1MongoDB::Examples(3)  User Contributed Perl Documentation MongoDB::Examples(3)
2
3
4

NAME

6       MongoDB::Examples - Some more advanced examples
7

DATABASE COMMANDS

9   Distinct
10       The distinct command returns all values for a given key in a
11       collection.  For example, suppose we had a collection with the
12       following documents ("_id" value ignored):
13
14           { 'name' => 'a', code => 1 }
15           { 'name' => 'b', code => 1 }
16           { 'name' => 'c', code => 2 }
17           { 'name' => 'd', code => "3" }
18
19       If we wanted to see all of values in the "code" field, we could run:
20
21           my $result = $db->run_command([
22              "distinct" => "collection_name",
23              "key"      => "code",
24              "query"    => {}
25           ]);
26
27       Notice that the arguments are in an array, to ensure that their order
28       is preserved.  You could also use a Tie::IxHash.
29
30       "query" is an optional argument, which can be used to only run
31       "distinct" on specific documents.  It takes a hash (or Tie::IxHash or
32       array) in the same form as "find($query)" in MongoDB::Collection.
33
34       Running "distinct" on the above collection would give you:
35
36           {
37               'ok' => '1',
38               'values' => [
39                             1,
40                             2,
41                             "3"
42                           ]
43           };
44
45   MapReduce
46       MapReduce is a powerful aggregation tool.  (For traditional queries,
47       you should use "MongoDB::Collection::query".)
48
49       This example counts the number of occurences of each tag in a
50       collection.  Each document contains a "tags" array that contains zero
51       or more strings.
52
53           my $map = <<MAP;
54           function() {
55               this.tags.forEach(function(tag) {
56                   emit(tag, {count : 1});
57               });
58           }
59           MAP
60
61           my $reduce = <<REDUCE;
62           function(prev, current) {
63               result = {count : 0};
64               current.forEach(function(item) {
65                   result.count += item.count;
66               });
67               return result;
68           }
69           REDUCE
70
71           my $cmd = Tie::IxHash->new("mapreduce" => "foo",
72               "map" => $map,
73               "reduce" => $reduce);
74
75           my $result = $db->run_command($cmd);
76
77       See the MongoDB documentation on MapReduce for more information
78       (<http://dochub.mongodb.org/core/mapreduce>).
79

UPDATING

81   Positional Operator
82       In MongoDB 1.3.4 and later, you can use positional operator, "$", to
83       update elements of an array.  For instance, suppose you have an array
84       of user information and you want to update a user's name.
85
86       A sample document in JavaScript:
87
88           {
89               "users" : [
90                   {
91                       "name" : "bill",
92                       "age" : 60
93                   },
94                   {
95                       "name" : "fred",
96                       "age" : 29
97                   },
98               ]
99           }
100
101       The update:
102
103           $coll->update({"users.name" => "fred"}, {'users.$.name' => "george"});
104
105       This will update the array so that the element containing "name" =>
106       "fred" now has "name" => "george".
107
108
109
110perl v5.12.3                      2010-11-19              MongoDB::Examples(3)
Impressum