MongoDB::Collection - A Mongo Collection
Core documentation on collections: http://dochub.mongodb.org/core/collections.
The name of the collection.
The full_name of the collection, including the namespace of the database it's in.
$name = MongoDB::Collection::to_index_string({age : 1});
Takes a Tie::IxHash, hash reference, or array reference. Converts it into an index string.
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.
$query
MongoDB::Cursor
Valid query attributes are:
Limit the number of results.
Skip a number of results.
Order results.
See also core documentation on querying: http://dochub.mongodb.org/core/find.
my $object = $collection->find_one({ name => 'Resi' }); my $object = $collection->find_one({ name => 'Resi' }, { name => 1, age => 1});
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). If $fields is specified, the resulting document will only include the fields given (and the _id field) which can cut down on wire traffic.
$fields
_id
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.
$object
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 return 0 if it did not. You should check MongoDB::Database::last_error to see the reason that the insert failed.
$options
MongoDB::Database::last_error
See also core documentation on insert: http://dochub.mongodb.org/core/insert.
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 return 0 if it did not. You should check $MongoDB::Database::last_error to see the reason that the insert failed.
$MongoDB::Database::last_error
$collection->update({'x' => 3}, {'$inc' => {'count' => -1} }, {"upsert" => 1, "multiple" => 1});
Updates an existing $object matching $criteria in the database.
$criteria
Returns 1 unless the safe option is set.
safe
update can take a hash reference of options. The options currently supported are:
update
upsert
multiple
See also core documentation on update: http://dochub.mongodb.org/core/update.
$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).
MongoDB::Collection::drop
remove can take a hash reference of options. The options currently supported are
remove
just_one
See also core documentation on remove: http://dochub.mongodb.org/core/remove.
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.
$keys
Tie::IxHash
If the safe option is not set, ensure_index will always return 1.
The second parameter gives index options. Available options are:
unique =
By default, indexes are not unique. To create a unique index, pass "unique" = true>. true can be boolean::true or any other true value.
"unique" =
true
drop_dups =
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.
safe =
If the update fails and safe is set, this function will return 0. You should check MongoDB::Database::last_error to find out why the update failed.
See also core documentation on indexing: http://dochub.mongodb.org/core/indexes.
$collection->save({"author" => "joe"}); my $post = $collection->find_one; $post->{author} = {"name" => "joe", "id" => 123, "phone" => "555-5555"}; $collection->save($post);
Inserts a document into the database if it does not have an _id field, upserts it if it does have an _id field.
If the save fails and safe is set, this function will return 0. You should check MongoDB::Database::last_error to find out why the update failed.
The return types for this function are a bit of a mess, as it will return the _id if a new document was inserted, 1 if an upsert occurred, and 0 if the safe option was set and an error occurred.
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.
$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.
info
$collection->drop_indexes;
Removes all indexes from this collection.
$collection->drop_index('foo_1');
Removes an index called $index_name from this collection. Use MongoDB::Collection::get_indexes to find the index name.
$index_name
MongoDB::Collection::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
name
key
{ '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.
dirX
$collection->drop;
Deletes a collection as well as all of its indexes.
Kristina Chodorow <kristina@mongodb.org>
To install MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDB
CPAN shell
perl -MCPAN -e shell install MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.