Aggregation pipeline with $or query and $sort (2.6)

197 views
Skip to first unread message

bradle...@twinehealth.com

unread,
Apr 23, 2014, 5:44:18 PM4/23/14
to mongod...@googlegroups.com

Greetings,

Since upgrading to 2.6 I am having an issue with an aggregation pipeline that includes an $or in the $match and a $sort in the pipeline.

Example:

db.createCollection('orAndSort');
db
.orAndSort.insert({name: "red", value: 1})
db
.orAndSort.insert({name: "red", value: 2})
db
.orAndSort.insert({name: "red", value: 3})
db
.orAndSort.insert({name: "blue", value: 10})
db
.orAndSort.insert({name: "blue", value: 20})
db
.orAndSort.insert({name: "green", value: 100})
db
.orAndSort.aggregate([
   
{ '$match': { '$or': [ { name: 'red' }, { name: 'blue' }] } },
   
{ '$sort': { value: 1 }}
]);




Result:

assert: command failed: {
 
"errmsg" : "exception: cursor encountered an error",
 
"code" : 17285,
 
"ok" : 0
} : aggregate failed
Error: command failed: {
 
"errmsg" : "exception: cursor encountered an error",
 
"code" : 17285,
 
"ok" : 0
} : aggregate failed
    at
Error (<anonymous>)
    at doassert
(src/mongo/shell/assert.js:11:14)
    at
Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
    at
DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
    at
(shell):1:14
2014-04-23T17:40:02.818-0400 Error: command failed: {
 
"errmsg" : "exception: cursor encountered an error",
 
"code" : 17285,
 
"ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13

Removing either the $sort or the $or condition from the $match causes the pipeline to execute successfully. Thank you for any pointers.

bradle...@twinehealth.com

unread,
Apr 24, 2014, 9:37:58 AM4/24/14
to mongod...@googlegroups.com
Follow-up,

I have discovered that adding a $project before the $sort avoids the error.

db.orAndSort.aggregate([
   
{ '$match': { '$or': [ { name: 'red' }, { name: 'blue' }] } },

   
{ '$project': {name: 1, value: 1}},
   
{ '$sort': { value: 1 }}
]);

In my actual application, this pattern is probably preferable anyway since I have no need to sort the complete documents. I would still be interested in knowing if this is a bug or other known issue. 

Mathias Stearn

unread,
Apr 24, 2014, 1:03:18 PM4/24/14
to mongod...@googlegroups.com
You discovered a bug. I went ahead and filed a ticket here: https://jira.mongodb.org/browse/SERVER-13715.

One other work around would be to add an index on {value: 1}. If you do that, it would actually be better to put the $project after the sort rather than before it.

Asya Kamsky

unread,
Apr 24, 2014, 4:46:42 PM4/24/14
to mongodb-user
Another work-around to the bug (in your particular case, since $or is using the same field in both expressions is to change it to:

db.orAndSort.aggregate([     { '$match': { name: {$in:[ 'red' , 'blue'] } } },     { '$sort': { value: 1 }} ]);
{ "_id" : ObjectId("535977d05168d6c47350f6b7"), "name" : "red", "value" : 1 }
{ "_id" : ObjectId("535977d05168d6c47350f6b8"), "name" : "red", "value" : 2 }
{ "_id" : ObjectId("535977d05168d6c47350f6b9"), "name" : "red", "value" : 3 }
{ "_id" : ObjectId("535977d05168d6c47350f6ba"), "name" : "blue", "value" : 10 }
{ "_id" : ObjectId("535977d05168d6c47350f6bb"), "name" : "blue", "value" : 20 }

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/bafa70e9-3b60-45e1-8ca0-5afb3e750660%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages