# # Copyright 2014 MongoDB, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # package MongoDB::BulkWriteView; # ABSTRACT: Bulk write operations against a query document use version; our $VERSION = 'v0.999.998.6'; # TRIAL use Moose; use Syntax::Keyword::Junction qw/any/; use MongoDB::Error; use MongoDB::_Types -types; use Types::Standard -types; use boolean; use namespace::clean -except => 'meta'; # A hash reference containing a MongoDB query document has _query => ( is => 'ro', isa => IxHash, coerce => 1, required => 1 ); # Originating bulk write object for executing write operations. has _bulk => ( is => 'ro', isa => InstanceOf['MongoDB::BulkWrite'], required => 1, handles => [qw/_enqueue_write/] ); has _upsert => ( is => 'ro', isa => Booleanpm, default => sub { false }, ); sub upsert { my ($self) = @_; unless ( @_ == 1 ) { MongoDB::UsageError->throw("the upsert method takes no arguments"); } return $self->new( %$self, _upsert => true ); } sub update { push @_, "update"; goto &_update; } sub update_one { push @_, "update_one"; goto &_update; } sub replace_one { push @_, "replace_one"; goto &_update; } sub _update { my $method = pop @_; my ( $self, $doc ) = @_; unless ( @_ == 2 && ref $doc eq any(qw/HASH ARRAY Tie::IxHash/) ) { MongoDB::UsageError->throw("argument to $method must be a single hashref, arrayref or Tie::IxHash"); } if ( ref $doc eq 'ARRAY' ) { MongoDB::UsageError->throw("array reference to $method must have key/value pairs") if @$doc % 2; $doc = Tie::IxHash->new(@$doc); } elsif ( ref $doc eq 'HASH' ) { $doc = Tie::IxHash->new(%$doc); } my $update = { q => $self->_query, u => $doc, multi => $method eq 'update' ? true : false, upsert => boolean( $self->_upsert ), is_replace => $method eq 'replace_one', }; $self->_enqueue_write( [ update => $update ] ); return; } sub remove { my ($self) = @_; $self->_enqueue_write( [ delete => { q => $self->_query, limit => 0 } ] ); return; } sub remove_one { my ($self) = @_; $self->_enqueue_write( [ delete => { q => $self->_query, limit => 1 } ] ); return; } __PACKAGE__->meta->make_immutable; 1; __END__ =pod =encoding UTF-8 =head1 NAME MongoDB::BulkWriteView - Bulk write operations against a query document =head1 VERSION version v0.999.998.6 =head1 SYNOPSIS my $bulk = $collection->initialize_ordered_bulk_op; # Update one document matching the selector bulk->find( { a => 1 } )->update_one( { '$inc' => { x => 1 } } ); # Update all documents matching the selector bulk->find( { a => 2 } )->update( { '$inc' => { x => 2 } } ); # Update all documents bulk->find( {} )->update( { '$inc' => { x => 2 } } ); # Replace entire document (update with whole doc replace) bulk->find( { a => 3 } )->replace_one( { x => 3 } ); # Update one document matching the selector or upsert bulk->find( { a => 1 } )->upsert()->update_one( { '$inc' => { x => 1 } } ); # Update all documents matching the selector or upsert bulk->find( { a => 2 } )->upsert()->update( { '$inc' => { x => 2 } } ); # Replaces a single document matching the selector or upsert bulk->find( { a => 3 } )->upsert()->replace_one( { x => 3 } ); # Remove a single document matching the selector bulk->find( { a => 4 } )->remove_one(); # Remove all documents matching the selector bulk->find( { a => 5 } )->remove(); # Remove all documents bulk->find( {} )->remove(); =head1 DESCRIPTION This class provides means to specify write operations constrained by a query document. To instantiate a C, use the L method from L or the L method described below. Except for L, all methods have an empty return on success; an exception will be thrown on error. =head1 METHODS =head2 remove $bulk->remove; Removes all documents matching the query document. =head2 remove_one $bulk->remove_one; Removes a single document matching the query document. =head2 replace_one $bulk->replace_one( $doc ); Replaces the document matching the query document. The document to replace must not have any keys that begin with a dollar sign, C<$>. =head2 update $bulk->update( $modification ); Updates all documents matching the query document. The modification document must have all its keys begin with a dollar sign, C<$>. =head2 update_one $bulk->update_one( $modification ); Updates a single document matching the query document. The modification document must have all its keys begin with a dollar sign, C<$>. =head2 upsert $bulk->upsert->replace_one( $doc ); Returns a new C object that will treat every update, update_one or replace_one operation as an upsert operation. =head1 AUTHORS =over 4 =item * David Golden =item * Mike Friedman =item * Kristina Chodorow =item * Florian Ragwitz =back =head1 COPYRIGHT AND LICENSE This software is Copyright (c) 2015 by MongoDB, Inc.. This is free software, licensed under: The Apache License, Version 2.0, January 2004 =cut