[SERVER-9604] Improve the error message if $last operator is used in _id field for $group operation Created: 07/May/13  Updated: 06/Dec/22  Resolved: 11/Aug/20

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Trivial - P5
Reporter: Linda Qin Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 0
Labels: qopt-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-9577 new deverloper question on usage of A... Closed
Assigned Teams:
Query
Participants:

 Description   

If $last operator is used in _id field for $group operation, for example:

db.test.aggregate([
    { $sort : { score : 1 }},
    { $group : {
               _id : { name : "$name",
                       highest : { $last : "$score" }}}
    }
])

the following error message is displayed:

{
	"errmsg" : "exception: invalid operator '$last'",
	"code" : 15999,
	"ok" : 0
}

The error message is not so helpful for the user to fix the issue. It would help if we had a more specific error message, i.e. not to use $last in the _id field.

Similarly for $first operator.

BTW, the correct usage should be:

db.test.aggregate([
    { $sort : { score : 1 }},
    { $group : {
               _id : { name : "$name" },
               highest : { $last : "$score" }}
    }
])



 Comments   
Comment by Charlie Swanson [ 11/Aug/20 ]

The error message has improved since filing as Asya mentioned, so closing as "Won't Do".

Comment by Asya Kamsky [ 03/Aug/20 ]

Given the direction is for all accumulators to eventually be expressions, the example case is no longer valid example for better error message... Consider closing as won't do?

Comment by Asya Kamsky [ 03/Aug/20 ]

We have now added an expression $last so the error they would get here would change to

"errmsg" : "$last's argument must be an array, but is <type-of-argument>

Comment by Charlie Swanson [ 16/Mar/18 ]

It doesn't seem crazy to me to add some logic to check if an accumulator name was used, or even a stage name. I'll be the first to admit that it's hard to construct these things, and there are many levels of nesting and curly braces making it even harder. I can't say how often this happens, but it seems to me that even if we build a great aggregation builder, this could still be an issue if someone has to then translate javascript into java or something like that which could be error prone. Also, I don't think we can assume everyone would use that.

The server work doesn't sound crazy difficult, but you make a fair point that it might not be that much more helpful to say "Unknown expression, but $last is the name of an accumulator if you meant that". I do feel that such a message is more clear that the error is in the way you constructed the pipeline, whereas the current error might lead you to believe your server version is outdated or perhaps your driver is outdated or something.

Comment by Asya Kamsky [ 15/Mar/18 ]

Currently the error is:

	"errmsg" : "Unrecognized expression '$last'",
	"code" : 168,
	"codeName" : "InvalidPipelineOperator"

I can't see us doing much more here since I don't think we know the invalid expression is inside _id we just know it's invalid (there is no aggregation expression $last, it's an accumulator).

david.storch or charlie.swanson do you think it makes sense once the token doesn't parse as legal expression to check if it is a legal accumulator and give a different error message (like $last is not a legal expression, it's an accumulator and cannot be used here?)

It feels like if someone doesn't realize what's wrong here, this won't help that much (they don't know what an accumulator is?) so I think it's better to work on improved ways to author aggregation pipelines (like with Compass, etc) and close this as won't fix.

Comment by Scott Hernandez (Inactive) [ 07/May/13 ]

What should the error message be? What would have helped?

Generated at Thu Feb 08 03:20:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.