【发布时间】:2013-03-13 18:14:10
【问题描述】:
假设我有以下内容:
this.aggregate(
{$unwind: "$tags"},
{$match: {tags: {$in: pip.activity.tags}}},
{$group : {_id : '$_id',matches:{$sum:1}}},
{$project: { _id: 0,matches:1}},
{$sort: {matches:-1 }},
callback
);
我将如何在结果中包含一个额外的“外部”objectId 字段?例如,如果我有以下情况:
var otherField = new ObjectId('xxxxxxx');
this.aggregate(
{$unwind: "$tags"},
{$match: {tags: {$in: pip.activity.tags}}},
{$group : {_id : '$_id',matches:{$sum:1}}},
{$project: { _id: 0,matches:1,otherField:otherField}}, <-- include otherField
{$sort: {matches:-1 }},
callback
);
这可能吗,或者我应该在这个特定步骤中使用 forLoop 或 MapReduce 吗?我正在寻找真正有效的东西。
【问题讨论】:
标签: mongodb aggregation-framework