Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit 08a24e7

Browse files
authored
Merge bd744e6 into 94cb815
2 parents 94cb815 + bd744e6 commit 08a24e7

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

lib/MongoDB/Collection.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,9 @@ sub __ixhash {
18571857
elsif ( $type eq 'ARRAY' ) {
18581858
$hash->{$key} = Tie::IxHash->new( @$ref );
18591859
}
1860+
elsif ( $ref->$_can('_as_tied_hash') ) {
1861+
$hash->{$key} = $ref->_as_tied_hash;
1862+
}
18601863
else {
18611864
MongoDB::UsageError->throw("Can't convert $type to a Tie::IxHash");
18621865
}

t/sort-bson-doc.t

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2015 - 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+
24+
use MongoDB;
25+
use MongoDB::Error;
26+
use BSON::Types ':all';
27+
28+
use lib "t/lib";
29+
use MongoDBTest qw/skip_unless_mongod build_client get_test_db server_version server_type get_capped/;
30+
31+
skip_unless_mongod();
32+
33+
my $conn = build_client();
34+
my $testdb = get_test_db($conn);
35+
my $server_version = server_version($conn);
36+
my $server_type = server_type($conn);
37+
my $coll = $testdb->get_collection('test_collection');
38+
39+
$coll->insert_many( [
40+
{ _id => 1, size => 10 },
41+
{ _id => 2, size => 5 },
42+
{ _id => 3, size => 15 },
43+
] );
44+
45+
subtest "sort standard hash" => sub {
46+
my @res = $coll->find( {}, { sort => { size => 1 } } )->result->all;
47+
48+
cmp_deeply \@res,
49+
[
50+
{ _id => 2, size => 5 },
51+
{ _id => 1, size => 10 },
52+
{ _id => 3, size => 15 },
53+
],
54+
'Got correct sort order';
55+
};
56+
57+
subtest "sort BSON::Doc" => sub {
58+
my $b_doc = bson_doc( size => 1 );
59+
my @res = $coll->find( {}, { sort => $b_doc } )->result->all;
60+
61+
cmp_deeply \@res,
62+
[
63+
{ _id => 2, size => 5 },
64+
{ _id => 1, size => 10 },
65+
{ _id => 3, size => 15 },
66+
],
67+
'Got correct sort order';
68+
};
69+
70+
done_testing;

0 commit comments

Comments
 (0)