[DOCS-11427] Docs for SERVER-27100: ExpressionObject does not optimize itself to a constant expression Created: 08/Mar/18  Updated: 29/Oct/23  Resolved: 29/May/18

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: 3.7.3

Type: Task Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: Andrew Aldridge
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
documents SERVER-27100 ExpressionObject does not optimize it... Closed
Participants:
Days since reply: 5 years, 37 weeks, 1 day ago
Epic Link: DOCS: 4.0 Server
Story Points: 0.25

 Description   

Documentation Request Summary:

There's a note on the $bucket docs page (in the 'boundaries' argument) about how documents must be wrapped in $literal, which is no longer true (so long as all the values are constants)

https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/

Engineering Ticket Description:

An ExpressionObject's implementation of optimize() is missing an optimization where if all of the values in the object are constant, the entire ExpressionObject can be replaced with an ExpressionConstant representing the object literal.

This can cause an confusing error message when using the $bucket stage, since $bucket ensures all entries of the boundaries array are constants:

> db.baz.find()
{ "_id" : ObjectId("582e293f38b7ba11ec92eda8"), "a" : { "b" : 1, "c" : 1 } }
{ "_id" : ObjectId("582e293f38b7ba11ec92eda9"), "a" : { "b" : 1, "c" : 2 } }
 
// This fails, implying the object literals are not constants:
> db.baz.aggregate([{$bucket: {groupBy: "$a", boundaries: [{b: 1, c: 1}, {b: 1, c: 2}, {b: 2, c: 3}]}}])
assert: command failed: {
	"ok" : 0,
	"errmsg" : "The $bucket 'boundaries' field must be an array of constant values, but found value: { b: 1.0, c: 1.0 }.",
	"code" : 40191,
	"codeName" : "Location40191"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1
 
2016-11-17T17:05:06.583-0500 E QUERY    [main] Error: command failed: {
	"ok" : 0,
	"errmsg" : "The $bucket 'boundaries' field must be an array of constant values, but found value: { b: 1.0, c: 1.0 }.",
	"code" : 40191,
	"codeName" : "Location40191"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1
 
// If the objects are wrapped in a $literal, the $bucket succeeds:
> db.baz.aggregate([{$bucket: {groupBy: "$a", boundaries: [{$literal: {b: 1, c: 1}}, {$literal: {b: 1, c: 2}}, {$literal: {b: 2, c: 3}}]}}])
{ "_id" : { "b" : 1, "c" : 1 }, "count" : 1 }
{ "_id" : { "b" : 1, "c" : 2 }, "count" : 1 }



 Comments   
Comment by Githook User [ 29/May/18 ]

Author:

{'username': 'i80and', 'name': 'Andrew Aldridge', 'email': 'i80and@foxquill.com'}

Message: DOCS-11427: Remove $bucket note about $literal
Branch: master
https://github.com/mongodb/docs/commit/3527a0c83b979bae1306e90d248470fda160fc07

Generated at Thu Feb 08 08:02:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.