AnyMongo
view release on metacpan or search on metacpan
lib/AnyMongo/Collection.pm view on Meta::CPAN
my ($self, $query, $options) = @_;
my $just_one;
my $conn = $self->_database->_connection;
my $ns = $self->full_name;
$query ||= {};
if (defined $options && ref $options eq 'HASH') {
$just_one = exists $options->{just_one} ? $options->{just_one} : 0;
if ($options->{safe}) {
my $ok = $self->_make_safe(AnyMongo::MongoSupport::build_remove_message(
AnyMongo::MongoSupport::make_request_id(),
$ns, $query, $just_one));
return $ok;
}
}
else {
$just_one = $options || 0;
}
$conn->send_message(AnyMongo::MongoSupport::build_remove_message(
AnyMongo::MongoSupport::make_request_id(),
$ns, $query, $just_one));
return 1;
}
sub ensure_index {
my ($self, $keys, $options, $garbage) = @_;
my $ns = $self->full_name;
# we need to use the crappy old api if...
# - $options isn't a hash, it's a string like "ascending"
# - $keys is a one-element array: [foo]
# - $keys is an array with more than one element and the second
# element isn't a direction (or at least a good one)
# - Tie::IxHash has values like "ascending"
if (($options && ref $options ne 'HASH') ||
(ref $keys eq 'ARRAY' &&
($#$keys == 0 || $#$keys >= 1 && !($keys->[1] =~ /-?1/))) ||
(ref $keys eq 'Tie::IxHash' && $keys->[2][0] =~ /(de|a)scending/)) {
Carp::croak("you're using the old ensure_index format, please upgrade");
}
my $obj = Tie::IxHash->new("ns" => $ns, "key" => $keys);
if (exists $options->{name}) {
$obj->Push("name" => $options->{name});
}
else {
$obj->Push("name" => AnyMongo::Collection::to_index_string($keys));
}
if (exists $options->{unique}) {
$obj->Push("unique" => ($options->{unique} ? boolean::true : boolean::false));
}
if (exists $options->{drop_dups}) {
$obj->Push("dropDups" => ($options->{drop_dups} ? boolean::true : boolean::false));
}
if (exists $options->{background}) {
$obj->Push("background" => ($options->{background} ? boolean::true : boolean::false));
}
my ($db, $coll) = $ns =~ m/^([^\.]+)\.(.*)/;
my $indexes = $self->_database->get_collection("system.indexes");
return $indexes->insert($obj, $options);
}
sub _make_safe {
my ($self, $req) = @_;
my $conn = $self->_database->_connection;
my $db = $self->_database->name;
my $last_error = Tie::IxHash->new(getlasterror => 1, w => $conn->w, wtimeout => $conn->wtimeout);
my $request_id = AnyMongo::MongoSupport::make_request_id();
my $query = AnyMongo::MongoSupport::build_query_message(
$request_id, $db.'.$cmd', 0, 0, -1, $last_error);
# $conn->recv($cursor);
$conn->send_message("$req$query");
my ($number_received,$cursor_id,$result) = $conn->recv_message();
# use Data::Dumper;
# warn "_make_safe getlasterror number_received:$number_received cursor_id:$cursor_id result=> ".Dumper($result);
if ( $number_received == 1 ) {
my $ok = $result->[0];
# $result->{ok} is 1 if err is set
Carp::croak $ok->{err} if $ok->{err};
# $result->{ok} == 0 is still an error
if (!$ok->{ok}) {
Carp::croak $ok->{errmsg};
}
}
return 1;
}
sub save {
my ($self, $doc, $options) = @_;
if (exists $doc->{"_id"}) {
if (!$options || !ref $options eq 'HASH') {
$options = {"upsert" => boolean::true};
}
else {
$options->{'upsert'} = boolean::true;
}
return $self->update({"_id" => $doc->{"_id"}}, $doc, $options);
}
else {
return $self->insert($doc, $options);
}
}
sub count {
my ($self, $query) = @_;
$query ||= {};
my $obj;
( run in 2.271 seconds using v1.01-cache-2.11-cpan-d8267643d1d )