在我遇到的一个Mongo聚合示例中,表达式
这是样本数据:
这里是代码:
我正在查看的代码是:
$$this
被使用,但在MongoDB文档中找不到对它的参考(甚至在聚合变量的文档中也没有)。这是样本数据:
{ "_id" : 1, "actions" : [ 2, 6, 3, 8, 5, 3 ] }
{ "_id" : 2, "actions" : [ 6, 4, 2, 8, 4, 3 ] }
{ "_id" : 3, "actions" : [ 6, 4, 6, 4, 3 ] }
{ "_id" : 4, "actions" : [ 6, 8, 3 ] }
{ "_id" : 5, "actions" : [ 6, 8 ] }
{ "_id" : 6, "actions" : [ 6, 3, 11, 8, 3 ] }
{ "_id" : 7, "actions" : [ 6, 3, 8 ] }
这里是代码:
我正在查看的代码是:
db.test.aggregate([
{$match:{actions:{$all:[6,3,8]}}},
{$project:{actions638:{$map:{
input:{$range:[0,{$subtract:[{$size:"$actions"},2]}]},
in:{$slice:["$actions","$$this",3]}
}}}}
])
这里是输出
{ "_id" : 1, "actions638" : [ [ 2, 6, 3 ], [ 6, 3, 8 ], [ 3, 8, 5 ], [ 8, 5, 3 ] ] }
{ "_id" : 2, "actions638" : [ [ 6, 4, 2 ], [ 4, 2, 8 ], [ 2, 8, 4 ], [ 8, 4, 3 ] ] }
{ "_id" : 4, "actions638" : [ [ 6, 8, 3 ] ] }
{ "_id" : 6, "actions638" : [ [ 6, 3, 11 ], [ 3, 11, 8 ], [ 11, 8, 3 ] ] }
{ "_id" : 7, "actions638" : [ [ 6, 3, 8 ] ] }
$$this
。 - CodyBugstein