在MongoDB 4.0和Mongoose最新版本中使用聚合时,我遇到了问题/限制。
通过该聚合,我得到了我想要的数据,但是如果2个或更多文档具有相同的数据,则重复该数据。 我想做的是:仅在键-类型对尚不存在的情况下,在项目中放入条件才能插入结果(在项目中)。 我能怎么做?我尝试了几种方法,但我做不到...
Call.aggregate([
{"$match":{ownerId: ownerId, current: true}},
{"$project":{"arrayofkeyvalue":{"$objectToArray":"$$ROOT"}}},
{"$unwind":"$arrayofkeyvalue"},
{"$project":
{
"_id":0,
"key":"$arrayofkeyvalue.k",
"type":{"$type":"$arrayofkeyvalue.v"}
}
}
]);
在输出中我得到:
[{ key: 'name', type: 'string' },{ key: 'name', type: 'string' },{ key: 'deleted', type: 'bool' },{ key: 'deleted', type: 'bool' }]
非常感谢!