[SERVER-34879] $let in the $lookup pipeline only allows variable names with lower cases Created: 07/May/18  Updated: 27/Oct/23  Resolved: 08/May/18

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

Type: Bug Priority: Major - P3
Reporter: Jun Liu Assignee: Andy Schwerin
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

In the mongodb document:

 

https://docs.mongodb.com/master/reference/operator/aggregation/lookup/index.html#lookup-uncorrelated-subuery

 

The example query:

db.orders.aggregate([
{
$lookup:

{ from: "warehouses", let: \{ order_item: "$item", order_qty: "$ordered" }

,
pipeline: [
{ $match:
{ $expr:

{ $and: [ \{ $eq: [ "$stock_item", "$$order_item" ] }

,
{ $gte: [ "$instock", "$$order_qty" ] }
]
}
}
},
{ $project:

{ stock_item: 0, _id: 0 }

}
],
as: "stockdata"
}
}
])

 

This runs fine, but if the line:

let: { order_item: "$item", order_qty: "$ordered" },

is replaced by:

let: { ORDER_ITEM: "$item", ORDER_QTY: "$ordered" },

and replace these variables in the other two lines:

 

{ $eq: [ "$stock_item", "$$ORDER_ITEM" ] }

,
{ $gte: [ "$instock", "$$ORDER_QTY" ] }

The mongo shell returns error:

 

assert: command failed:

{ "ok" : 0, "errmsg" : "'ORDER_ITEM' starts with an invalid character for a user variable name", "code" : 16867, "codeName" : "Location16867" }

: 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:403:5

DB.prototype._runAggregate@src/mongo/shell/db.js:260:9

DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1212:12

@(shell):1:1

 

2018-05-07T18:11:21.195-0400 E QUERY    [thread1] Error: command failed:

{ "ok" : 0, "errmsg" : "'ORDER_ITEM' starts with an invalid character for a user variable name", "code" : 16867, "codeName" : "Location16867" }

: 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:403:5

DB.prototype._runAggregate@src/mongo/shell/db.js:260:9

DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1212:12

@(shell):1:1

 

 

 



 Comments   
Comment by Andy Schwerin [ 08/May/18 ]

Per the documentation on user variables in aggregation pipelines, user defined variables must start with a lower case letters a-z or with non-ascii characters.

Generated at Thu Feb 08 04:38:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.