1MongoDB::Collection(3)User Contributed Perl DocumentationMongoDB::Collection(3)
2
3
4

NAME

6       MongoDB::Collection - A Mongo collection
7

SYNOPSIS

9       An instance of a MongoDB collection.
10
11           # gets the foo collection
12           my $collection = $db->foo;
13
14       Collection names can be chained together to access subcollections.  For
15       instance, the collection "foo.bar" can be accessed with:
16
17           my $collection = $db->foo->bar;
18
19       You can also access collections with the "get_collection" in
20       MongoDB::Database method.
21

SEE ALSO

23       Core documentation on collections:
24       <http://dochub.mongodb.org/core/collections>.
25

ATTRIBUTES

27   name
28       The name of the collection.
29
30   full_name
31       The full_name of the collection, including the namespace of the
32       database it's in.
33

STATIC METHODS

35   to_index_string ($keys)
36           $name = MongoDB::Collection::to_index_string({age : 1});
37
38       Takes a Tie::IxHash, hash reference, or array reference.  Converts it
39       into an index string.
40

METHODS

42   find($query)
43           my $cursor = $collection->find({ i => { '$gt' => 42 } });
44
45       Executes the given $query and returns a "MongoDB::Cursor" with the
46       results.  $query can be a hash reference, Tie::IxHash, or array
47       reference (with an even number of elements).
48
49       The set of fields returned can be limited through the use of the
50       "MongoDB::Cursor::fields" method on the resulting MongoDB::Cursor
51       object.  Other commonly used cursor methods are
52       "MongoDB::Cursor::limit", "MongoDB::Cursor::skip", and
53       "MongoDB::Cursor::sort".
54
55       See also core documentation on querying:
56       <http://dochub.mongodb.org/core/find>.
57
58   query($query, $attrs?)
59       Identical to "MongoDB::Collection::find", described above.
60
61           my $cursor = $collection->query({ }, { limit => 10, skip => 10 });
62
63           my $cursor = $collection->query(
64               { location => "Vancouver" },
65               { sort_by  => { age => 1 } },
66           );
67
68       Valid query attributes are:
69
70       limit
71           Limit the number of results.
72
73       skip
74           Skip a number of results.
75
76       sort_by
77           Order results.
78
79   find_one ($query, $fields?)
80           my $object = $collection->find_one({ name => 'Resi' });
81           my $object = $collection->find_one({ name => 'Resi' }, { name => 1, age => 1});
82
83       Executes the given $query and returns the first object matching it.
84       $query can be a hash reference, Tie::IxHash, or array reference (with
85       an even number of elements).  If $fields is specified, the resulting
86       document will only include the fields given (and the "_id" field) which
87       can cut down on wire traffic.
88
89   insert ($object, $options?)
90           my $id1 = $coll->insert({ name => 'mongo', type => 'database' });
91           my $id2 = $coll->insert({ name => 'mongo', type => 'database' }, {safe => 1});
92
93       Inserts the given $object into the database and returns it's id value.
94       $object can be a hash reference, a reference to an array with an even
95       number of elements, or a Tie::IxHash.  The id is the "_id" value
96       specified in the data or a MongoDB::OID.
97
98       The optional $options parameter can be used to specify if this is a
99       safe insert.  A safe insert will check with the database if the insert
100       succeeded and croak if it did not.  You can also check if the insert
101       succeeded by doing an unsafe insert, then calling
102       "last_error($options?)" in MongoDB::Database.
103
104       See also core documentation on insert:
105       <http://dochub.mongodb.org/core/insert>.
106
107   batch_insert (\@array, $options)
108           my @ids = $collection->batch_insert([{name => "Joe"}, {name => "Fred"}, {name => "Sam"}]);
109
110       Inserts each of the documents in the array into the database and
111       returns an array of their _id fields.
112
113       The optional $options parameter can be used to specify if this is a
114       safe insert.  A safe insert will check with the database if the insert
115       succeeded and croak if it did not. You can also check if the inserts
116       succeeded by doing an unsafe batch insert, then calling
117       "last_error($options?)" in MongoDB::Database.
118
119   update (\%criteria, \%object, \%options?)
120           $collection->update({'x' => 3}, {'$inc' => {'count' => -1} }, {"upsert" => 1, "multiple" => 1});
121
122       Updates an existing $object matching $criteria in the database.
123
124       Returns 1 unless the "safe" option is set. If "safe" is set, this will
125       return a hash of information about the update, including number of
126       documents updated ("n").  If "safe" is set and the update fails,
127       "update" will croak. You can also check if the update succeeded by
128       doing an unsafe update, then calling "last_error($options?)" in
129       MongoDB::Database.
130
131       "update" can take a hash reference of options.  The options currently
132       supported are:
133
134       "upsert" If no object matching $criteria is found, $object will be
135       inserted.
136       "multiple" All of the documents that match $criteria will be updated,
137       not just the first document found. (Only available with database
138       version 1.1.3 and newer.)
139       "safe" If the update fails and safe is set, the update will croak.
140
141       See also core documentation on update:
142       <http://dochub.mongodb.org/core/update>.
143
144   remove ($query?, $options?)
145           $collection->remove({ answer => { '$ne' => 42 } });
146
147       Removes all objects matching the given $query from the database. If no
148       parameters are given, removes all objects from the collection (but does
149       not delete indexes, as "MongoDB::Collection::drop" does).
150
151       Returns 1 unless the "safe" option is set.  If "safe" is set and the
152       remove succeeds, "remove" will return a hash of information about the
153       remove, including how many documents were removed ("n").  If the remove
154       fails and "safe" is set, "remove" will croak.  You can also check if
155       the remove succeeded by doing an unsafe remove, then calling
156       "last_error($options?)" in MongoDB::Database.
157
158       "remove" can take a hash reference of options.  The options currently
159       supported are
160
161       "just_one" Only one matching document to be removed.
162       "safe" If the update fails and safe is set, this function will croak.
163
164       See also core documentation on remove:
165       <http://dochub.mongodb.org/core/remove>.
166
167   ensure_index ($keys, $options?)
168           use boolean;
169           $collection->ensure_index({"foo" => 1, "bar" => -1}, { unique => true });
170
171       Makes sure the given $keys of this collection are indexed. $keys can be
172       an array reference, hash reference, or "Tie::IxHash".  "Tie::IxHash" is
173       prefered for multi-key indexes, so that the keys are in the correct
174       order.  1 creates an ascending index, -1 creates a descending index.
175
176       If the "safe" option is not set, "ensure_index" will not return
177       anything unless there is a socket error (in which case it will croak).
178       If the "safe" option is set and the index creation fails, it will also
179       croak. You can also check if the indexing succeeded by doing an unsafe
180       index creation, then calling "last_error($options?)" in
181       MongoDB::Database.
182
183       See the MongoDB::Indexing pod for more information on indexing.
184
185   save($doc, $options)
186           $collection->save({"author" => "joe"});
187           my $post = $collection->find_one;
188
189           $post->{author} = {"name" => "joe", "id" => 123, "phone" => "555-5555"};
190
191           $collection->save($post);
192
193       Inserts a document into the database if it does not have an _id field,
194       upserts it if it does have an _id field.
195
196       "safe =" boolean>
197           If the save fails and safe is set, this function will croak.
198
199       The return types for this function are a bit of a mess, as it will
200       return the _id if a new document was inserted, 1 if an upsert occurred,
201       and croak if the safe option was set and an error occurred.  You can
202       also check if the save succeeded by doing an unsafe save, then calling
203       "last_error($options?)" in MongoDB::Database.
204
205   count($query?)
206           my $n_objects = $collection->count({ name => 'Bob' });
207
208       Counts the number of objects in this collection that match the given
209       $query.  If no query is given, the total number of objects in the
210       collection is returned.
211
212   validate
213           $collection->validate;
214
215       Asks the server to validate this collection.  Returns a hash of the
216       form:
217
218           {
219               'ok' => '1',
220               'ns' => 'foo.bar',
221               'result' => info
222           }
223
224       where "info" is a string of information about the collection.
225
226   drop_indexes
227           $collection->drop_indexes;
228
229       Removes all indexes from this collection.
230
231   drop_index ($index_name)
232           $collection->drop_index('foo_1');
233
234       Removes an index called $index_name from this collection.  Use
235       "MongoDB::Collection::get_indexes" to find the index name.
236
237   get_indexes
238           my @indexes = $collection->get_indexes;
239
240       Returns a list of all indexes of this collection.  Each index contains
241       "ns", "name", and "key" fields of the form:
242
243           {
244               'ns' => 'db_name.collection_name',
245               'name' => 'index_name',
246               'key' => {
247                   'key1' => dir1,
248                   'key2' => dir2,
249                   ...
250                   'keyN' => dirN
251               }
252           }
253
254       where "dirX" is 1 or -1, depending on if the index is ascending or
255       descending on that key.
256
257   drop
258           $collection->drop;
259
260       Deletes a collection as well as all of its indexes.
261

AUTHOR

263         Kristina Chodorow <kristina@mongodb.org>
264
265
266
267perl v5.12.3                      2011-01-19            MongoDB::Collection(3)
Impressum