消息实际上说明了一切,因为您通过 $group 生成的“复合”_id 值实际上在将发布的 clientCollection 输出中不受支持。
当然,简单的解决方案是不使用来自$group 的_id 值作为生成输出中的“最终”_id 值。因此,正如 project README 上的示例所示,只需添加一个 $project 即可删除 _id 并将当前的“复合分组键”重命名为不同的属性名称:
ReactiveAggregate(this, Questionaire,
[
{
"$match": {
"time": {$gte: fromDate, $lte: toDate},
"userId": {'$regex' : regex}
}
},
{
$group : {
"_id": {
"userId": "$userId",
"date": { $dateToString: { format: "%Y-%m-%d", date: "$time" } }
},
"total": { "$sum": 1 }
}
},
// Add the reshaping to the end of the pipeline
{
"$project": {
"_id": 0, // remove the _id, this will be automatically filled
"userDate": "$_id", // the renamed compound key
"total": 1
}
}
], { clientCollection: "Questionaire" }
);
字段顺序会有所不同,因为 MongoDB 会保留现有字段(即本示例中的 "total"),然后将任何新字段添加到文档中。您可以通过在 $group 和 $project 阶段而不是 1 包含语法中使用不同的字段名称来计算这一点。
如果没有这样的插件,这种重塑是有 been regularly done in the past 的东西,通过再次重命名输出 _id 并提供一个新的 _id 值,该值与流星客户端集合期望出现在此属性中的内容兼容。
仔细检查how the code is implemented,最好在结果中实际提供_id 值,因为插件实际上不会创建_id 值。
因此,只需在分组中提取现有文档_id 值之一就足够了。所以我会添加一个$max 来执行此操作,然后替换$project 中的_id:
ReactiveAggregate(this, Questionaire,
[
{
"$match": {
"time": {$gte: fromDate, $lte: toDate},
"userId": {'$regex' : regex}
}
},
{
$group : {
"_id": {
"userId": "$userId",
"date": { $dateToString: { format: "%Y-%m-%d", date: "$time" } }
},
"maxId": { "$max": "$_id" },
"total": { "$sum": 1 }
}
},
// Add the reshaping to the end of the pipeline
{
"$project": {
"_id": "$maxId", // replaced _id
"userDate": "$_id", // the renamed compound key
"total": 1
}
}
], { clientCollection: "Questionaire" }
);
replacing the lines 可以在插件中轻松修补此问题
if (!sub._ids[doc._id]) {
sub.added(options.clientCollection, doc._id, doc);
} else {
sub.changed(options.clientCollection, doc._id, doc);
}
当管道输出的文档尚不存在_id 值时,使用Random.id():
if (!sub._ids[doc._id]) {
sub.added(options.clientCollection, doc._id || Random.id(), doc);
} else {
sub.changed(options.clientCollection, doc._id || Random.id(), doc);
}
但这可能是作者考虑更新软件包的注意事项。