NAME
MongoDB::Collection - A Mongo Collection
ATTRIBUTES
name
The name of the collection.
full_name
The full_name of the collection, including the namespace of the database it's in.
STATIC METHODS
to_index_string ($keys)
$name = MongoDB::Collection::to_index_string({age : 1});
Takes a Tie::IxHash, hash reference, or array reference. Converts it into an index string.
METHODS
query ($query, \%attrs?)
my $cursor = $collection->query({ i => { '$gt' => 42 } });
my $cursor = $collection->query({ }, { limit => 10, skip => 10 });
my $cursor = $collection->query(
{ location => "Vancouver" },
{ sort_by => { age => 1 } },
);
Executes the given $query
and returns a MongoDB::Cursor
with the results. $query
can be a hash reference, Tie::IxHash, or array reference (with an even number of elements). A hash reference of attributes may be passed as the second argument.
Valid query attributes are:
- limit
-
Limit the number of results.
- skip
-
Skip a number of results.
- sort_by
-
Order results.
find_one (\%query)
my $object = $collection->find_one({ name => 'Resi' });
Executes the given $query
and returns the first object matching it. $query
can be a hash reference, Tie::IxHash, or array reference (with an even number of elements).
insert (\%object, \%options?)
my $id1 = $coll->insert({ name => 'mongo', type => 'database' });
my $id2 = $coll->insert({ name => 'mongo', type => 'database' }, {safe => 1});
Inserts the given $object
into the database and returns it's id value. $object
can be a hash reference, a reference to an array with an even number of elements, or a Tie::IxHash. The id is the _id
value specified in the data or a MongoDB::OID.
The optional $options
parameter can be used to specify if this is a safe insert. A safe insert will check with the database if the insert succeeded and confess that it did not, if an error occured.
batch_insert (\@array, \%options)
my @ids = $collection->batch_insert([{name => "Joe"}, {name => "Fred"}, {name => "Sam"}]);
Inserts each of the documents in the array into the database and returns an array of their _id fields.
The optional $options
parameter can be used to specify if this is a safe insert. A safe insert will check with the database if the insert succeeded and confess that it did not, if an error occured.
update (\%criteria, \%object, \%options?)
$collection->update({'x' => 3}, {'$inc' => {'count' => -1} }, {"upsert" => 1, "multiple" => 1});
Updates an existing $object
matching $criteria
in the database.
update
can take a hash reference of options. The options currently supported are:
upsert
If no object matching$criteria
is found,$object
will be inserted.multiple
All of the documents that match$criteria
will be updated, not just the first document found. (Only available with database version 1.1.3 and newer.)
remove (\%query?, $just_one?)
$collection->remove({ answer => { '$ne' => 42 } });
Removes all objects matching the given $query
from the database. If no parameters are given, removes all objects from the collection (but does not delete indexes, as MongoDB::Collection::drop
does). Boolean parameter $just_one
causes only one matching document to be removed.
ensure_index (\%keys, $options?)
use boolean;
$collection->ensure_index({"foo" => 1, "bar" => -1}, { unique => true });
Makes sure the given $keys
of this collection are indexed. $keys
can be an array reference, hash reference, or Tie::IxHash
. Tie::IxHash
is prefered for multi-key indexes, so that the keys are in the correct order. 1 creates an ascending index, -1 creates a descending index.
The second parameter gives index options. Available options are:
unique =
boolean>-
By default, indexes are not unique. To create a unique index, pass
"unique" =
true>.true
can be boolean::true or any other true value. drop_dups =
boolean>-
If a unique index is being created on an existing set of data that has duplicate values, creating the index will fail. To force the index creation by deleting duplicate values, use this option. Again, any value that evaluates to true will work.
count ($query?)
my $n_objects = $collection->count({ name => 'Bob' });
Counts the number of objects in this collection that match the given $query
. If no query is given, the total number of objects in the collection is returned.
validate
$collection->validate;
Asks the server to validate this collection. Returns a hash of the form:
{
'ok' => '1',
'ns' => 'foo.bar',
'result' => info
}
where info
is a string of information about the collection.
drop_indexes
$collection->drop_indexes;
Removes all indexes from this collection.
drop_index ($index_name)
$collection->drop_index('foo_1');
Removes an index called $index_name
from this collection. Use MongoDB::Collection::get_indexes
to find the index name.
get_indexes
my @indexes = $collection->get_indexes;
Returns a list of all indexes of this collection. Each index contains ns
, name
, and key
fields of the form:
{
'ns' => 'db_name.collection_name',
'name' => 'index_name',
'key' => {
'key1' => dir1,
'key2' => dir2,
...
'keyN' => dirN
}
}
where dirX
is 1 or -1, depending on if the index is ascending or descending on that key.
drop
$collection->drop;
Deletes a collection as well as all of its indexes.
AUTHOR
Kristina Chodorow <kristina@mongodb.org>