【发布时间】:2019-02-22 14:01:00
【问题描述】:
也许有人可以帮助我解决使用 mongodb .Net 驱动程序将 mongodb 聚合查询转换为 C# 的问题。
根据我的问题here我尝试将以下内容转换为C#
db.getCollection('test').aggregate([
{ "$facet": {
"allInRoot1": [{
"$match": { "rootReferenceId": LUUID("9f3a73df-bca7-48b7-b111-285359e50a02") }
}],
"allInRoot2": [{
"$match": { "rootReferenceId": LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9") }
}]
}},
{ "$project": {
"difference": {
"$filter": {
"input": "$allInRoot1",
"as": "this",
"cond": { "$in": [ "$$this.reference.id", { "$setDifference": [ "$allInRoot1.reference.id", "$allInRoot2.reference.id" ] } ] }
}
}
}}
])
到目前为止我有这个
var matchFilterOne = new ExpressionFilterDefinition<NodeModel>(node => node.RootReferenceId == baseId);
var matchStageOne = PipelineStageDefinitionBuilder.Match(matchFilterOne);
var pipelineOne = PipelineDefinition<NodeModel, NodeModel>.Create(new IPipelineStageDefinition[] { matchStageOne });
var matchFilterTwo = new ExpressionFilterDefinition<NodeModel>(node => node.RootReferenceId == idToExclude);
var matchStageTwo = PipelineStageDefinitionBuilder.Match(matchFilterTwo);
var pipelineTwo = PipelineDefinition<NodeModel, NodeModel>.Create(new IPipelineStageDefinition[] { matchStageTwo });
var facetPipelineOne = AggregateFacet.Create("allInRoot1", pipelineOne);
var facetPipelineTwo = AggregateFacet.Create("allInRoot2", pipelineTwo);
var test = testCollection.Aggregate()
.Facet(facetPipelineOne, facetPipelineTwo)
/* This seems to fail because the facet structure is wrong and it can't access the $allInRoot1 field ...
.Project(@"{
'difference': {
'$filter': {
'input': '$allInRoot1',
'as': 'this',
'cond': {
'$in': [ '$$this.reference.id', { '$setDifference': [ '$allInRoot1.reference.id', '$allInRoot2.reference.id' ] }]
}
}}}")
*/
.FirstOrDefault();
也许有人有线索指出我正确的方向?是否也可以将投影与类型一起使用?
感谢任何帮助!
【问题讨论】:
-
使用 c# 创建一个 mongo 集合然后你可以使用 Linq 或 Lambda 表达式进行查询
-
哦,我已经有一个 mongo 集合并且正在使用它。在代码中是 testCollection。
-
您似乎擅长 mongo 查询,在这种情况下,您无需将查询转换为 Linq 或 lambda 表达式。
-
是的,你是对的。也许我应该按原样使用查询。大多数类型无论如何都不能很好地工作。例如。投影阶段内的 AggregatedFacetResults。我不确定它是否兼容。
标签: c# .net mongodb aggregation