我有以下系列:
{ "name": "name 1", "type": 1, description: "desc 1", "version": 1}
{ "name": "name 1", "type": 1, description: "desc 1 updated","version": 2}
{ "name": "name 2", "type": 1, description: "desc 2", "version": 1}
{ "name": "name 2", "type": 1, description: "desc 2 updated","version": 2}
每次需要使用相同的"名称"添加新项目时和"键入"将创建一个新版本。例如,如果我需要添加:
{ "name": "name 2", "type": 1, description: "desc 2 updated again" }
然后我的更新集合将如下所示:
{ "name": "name 1", "type": 1, description: "desc 1", "version": 1}
{ "name": "name 1", "type": 1, description: "desc 1 updated","version": 2}
{ "name": "name 2", "type": 1, description: "desc 2", "version": 1}
{ "name": "name 2", "type": 1, description: "desc 2 updated","version": 2}
{ "name": "name 2", "type": 1, description: "desc 2 updated again","version": 3}
我想要的是实现一个返回最新版本元素的查询,除非指定了特定的版本。
因此,默认情况下,查询应返回:
{ "name": "name 1", "type": 1, description: "desc 1 updated","version": 2}
{ "name": "name 2", "type": 1, description: "desc 2 updated again","version": 3}
或者如果我指定一个版本,例如2,那么它应该返回:
{ "name": "name 1", "type": 1, description: "desc 1 updated","version": 2}
{ "name": "name 2", "type": 1, description: "desc 2 updated","version": 2}
到目前为止,我已经达到了可以将计数分组到一个字段的程度:
_mongoDB.Collection.Aggregate()
.Match(filter)
.Group(new BsonDocument
{
{ "_id", "$name"},
{ "count", new BsonDocument("$sum", 1)}
});
到目前为止,我使用Filter Definition Builder构建过滤器。