Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit c40e1e1

Browse files
TBSliverxdg
authored andcommitted
PERL-1127 Support Pipelines in BulkWriteView
1 parent 9105073 commit c40e1e1

File tree

3 files changed

+165
-4
lines changed

3 files changed

+165
-4
lines changed

‎lib/MongoDB/BulkWriteView.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ sub _update {
107107
my ( $self, $doc ) = @_;
108108

109109
my $type = ref $doc;
110-
unless ( @_ == 2 && grep { $type eq $_ } qw/HASH ARRAY Tie::IxHash/ ) {
111-
MongoDB::UsageError->throw("argument to $method must be a single hashref, arrayref or Tie::IxHash");
110+
unless ( @_ == 2 && grep { $type eq $_ } qw/HASH ARRAY Tie::IxHash BSON::Array/ ) {
111+
MongoDB::UsageError->throw("argument to $method must be a single hashref, arrayref, Tie::IxHash or BSON::Array");
112112
}
113113

114114
if ( ref $doc eq 'ARRAY' ) {

‎t/bulk.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ for my $method (qw/initialize_ordered_bulk_op initialize_unordered_bulk_op/) {
222222
for my $k ( sort keys %bad_args ) {
223223
like(
224224
exception { $bulk->find( {} )->$update( @{ $bad_args{$k} } ) },
225-
qr/argument to $update must be a single hashref, arrayref or Tie::IxHash/,
225+
qr/argument to $update must be a single hashref, arrayref, Tie::IxHash or BSON::Array/,
226226
"$update( $k ) throws an error"
227227
);
228228
}
@@ -381,7 +381,7 @@ for my $method (qw/initialize_ordered_bulk_op initialize_unordered_bulk_op/) {
381381
for my $k ( sort keys %bad_args ) {
382382
like(
383383
exception { $bulk->find( {} )->replace_one( @{ $bad_args{$k} } ) },
384-
qr/argument to replace_one must be a single hashref, arrayref or Tie::IxHash/,
384+
qr/argument to replace_one must be a single hashref, arrayref, Tie::IxHash or BSON::Array/,
385385
"replace_one( $k ) throws an error"
386386
);
387387
}

‎t/bulk_write_pipeline.t

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Copyright 2019 - present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
use strict;
16+
use warnings;
17+
use Test::More 0.96;
18+
use Test::Fatal;
19+
use Test::Deep qw/!blessed/;
20+
21+
use utf8;
22+
use Tie::IxHash;
23+
use Encode qw(encode decode);
24+
use MongoDB::Error;
25+
26+
use MongoDB;
27+
use BSON::Types ':all';
28+
29+
use lib "t/lib";
30+
use MongoDBTest qw/
31+
skip_unless_mongod
32+
build_client
33+
get_test_db
34+
skip_unless_min_version
35+
/;
36+
37+
skip_unless_mongod();
38+
39+
my $conn = build_client();
40+
my $testdb = get_test_db($conn);
41+
42+
skip_unless_min_version($conn, 'v4.1.11');
43+
44+
my @tests = (
45+
{
46+
name => 'BulkWrite Update One Pipeline',
47+
input => [
48+
{
49+
"_id" => 1,
50+
"x" => 1,
51+
"y" => 1,
52+
"t" => { "u" => { "v" => 1 } }
53+
},
54+
{
55+
"_id" => 2,
56+
"x" => 2,
57+
"y" => 1
58+
},
59+
],
60+
method => 'update_one',
61+
find => { _id => 1 },
62+
pipeline => bson_array(
63+
{ '$replaceRoot' => { "newRoot" => '$t' } },
64+
{ '$addFields' => { "foo" => 1 } },
65+
),
66+
output => [
67+
{
68+
_id => 1,
69+
foo => 1,
70+
u => {
71+
v => 1,
72+
},
73+
},
74+
{
75+
_id => 2,
76+
x => 2,
77+
y => 1,
78+
},
79+
]
80+
},
81+
{
82+
name => 'BulkWrite Update Many Pipeline',
83+
input => [
84+
{
85+
"_id" => 1,
86+
"x" => 1,
87+
"y" => 1,
88+
"t" => { "u" => { "v" => 1 } }
89+
},
90+
{
91+
"_id" => 2,
92+
"x" => 1,
93+
"y" => 1,
94+
"t" => { "u" => { "v" => 1 } }
95+
},
96+
{
97+
"_id" => 3,
98+
"x" => 1,
99+
"y" => 1,
100+
"t" => { "u" => { "v" => 1 } }
101+
},
102+
{
103+
"_id" => 4,
104+
"x" => 2,
105+
"y" => 1
106+
},
107+
],
108+
method => 'update_many',
109+
find => { x => 1 },
110+
pipeline => bson_array(
111+
{ '$replaceRoot' => { "newRoot" => '$t' } },
112+
{ '$addFields' => { "foo" => 1 } },
113+
),
114+
output => [
115+
{
116+
_id => 1,
117+
foo => 1,
118+
u => {
119+
v => 1,
120+
},
121+
},
122+
{
123+
_id => 2,
124+
foo => 1,
125+
u => {
126+
v => 1,
127+
},
128+
},
129+
{
130+
_id => 3,
131+
foo => 1,
132+
u => {
133+
v => 1,
134+
},
135+
},
136+
{
137+
_id => 4,
138+
x => 2,
139+
y => 1,
140+
},
141+
]
142+
}
143+
);
144+
145+
for my $test ( @tests ) {
146+
my $coll = $testdb->get_collection('test_collection');
147+
subtest $test->{name} => sub {
148+
$coll->insert_many($test->{input});
149+
my $bulk = $coll->ordered_bulk;
150+
my $method = $test->{method};
151+
$bulk->find($test->{find})->$method($test->{pipeline});
152+
$bulk->execute;
153+
154+
my @output = $coll->find({})->all;
155+
156+
is_deeply( \@output, $test->{output}, 'Output as expected' );
157+
};
158+
$coll->drop;
159+
}
160+
161+
done_testing;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /