I am having trouble accessing an associative array "toPostID" in the following aggregate statement:
I'd like to use a custom user variable to determine my group_by's key.
db.comments.aggregate([
{
'$group' : {
'_id' : {
'$let' : {
'vars' : {
'toPostID': {
'$literal': {id1: 123, id2: 456},
}
},
'in': '$$toPostID[$id]',
}
},
'comments': {
'$push': '$$CURRENT',
}
}
}
]);
Here is an example of a comment:
{
id: 'id1',
text: 'hello'
}
I get the error: toPostID[$id]' contains an invalid character for a variable name: '['"
Is there a way to evaluate the array access?
I don't believe there's a way to access an object by a variable key in the aggregation pipeline like that. What is the purpose of this aggregation? From the example, it looks like you want to simply group together documents that have the same id - that's all that this pipeline would do if your notation worked - so why not just use "_id" : "$id" in the aggregation?
댓글 없음:
댓글 쓰기