我想你想要一个
Upsert with Modifier
。这将满足您的第二种方法,因此如果不存在匹配的行,则插入一行,或者仅在匹配的行存在时添加该值。
所以你的例子是这样的:
db.mycollection.update( { dimension1: a, … dimension5: e }, { $inc: { value: x } }, true); //the ‘true’ is the param that makes this update an upsert
</code>
如果要插入所有单个值,然后稍后聚合它们,我建议您将它们聚合到一个单独的集合中(以避免混淆)。可能最简单的方法是使用map / reduce而不是group,因为你可以简单地设置map / reduce的输出选项,将其输出合并到聚合集合中,其选项如下:
out : {reduce: “aggregatedcollection”}
。