Some problem with Perl driver and aggregation

16 views
Skip to first unread message

Roman Rakhman

unread,
May 22, 2015, 6:48:27 PM5/22/15
to mongod...@googlegroups.com
Hi, mongodb-users.

I have collection 'books': 
{
    "_id" : ObjectId("54901b078b823f2014080700"),
    "title" : "title1",
    "available" : 0,
    "time" : 14318789245,
    "score" : 1,
   "categories" : [ 
        "fiction", 
        "humor"
    ]
},
{
    "_id" : ObjectId("54901b078b823f2014080701"),
    "title" : "title2",
    "available" : 1,
    "time" : 14318789745,
    "score" : 1,
    "score_fiction": 5,
    "categories" : [ 
        "fiction", 
    ]
}
...
{
    "_id" : ObjectId("54901b078b823f2014080751"),
    "title" : "titleN",
    "available" : 1,
    "time" : 14318789545,
    "score" : 4,
    "score_fiction": 2,
}

I will aggregate it with this script:
my $books = $mongo->db('test')->get_collection('books')->aggregate(
    [
    {'$match' => {categories => fiction}},
    {'$project' => {
                 'av' => {'$cmp' => ['$available', 0] },
                 title => 1,
                 scr => {'$ifNull' => ['$score_fiction' , '$score'] },
                 time =>1}
    },
    {'$sort' => {av => -1, scr => -1, time => -1}},
    {'$limit' =>  10}
    ], {cursor => 1}
);

while (my $book = $books->next) {
print $book->{title} . " [" . $book->{scr} . "]\n";

After 5-7 runs I get two different results: 
1. With sorting by required fields. 
2. Without sorting. (possibly with sorting by time or the "$natural" time of addition).

When I run this aggregation pipeline in mongo's shell - it works with correctly sorting.
db.books.aggregate([{'$match' : {categories : 'fiction'},
{'$project' : {'av' : {'$cmp' : ['$available', 0] },
  title : 1,
  scr : {'$ifNull' : ['$score_fiction' , '$score'] },
  time:1}},
{'$sort' : {av : -1,scr :-1, time : -1}},
{'$limit':  10}]);

Is the problem in the driver or in my head?
PS
Perl driver MongoDB v0.708.1.0 
Mongo v 3.0.3
 
Reply all
Reply to author
Forward
0 new messages