MongoDB Fluent 聚合条件阶段

MongoDB Fluent Aggregate Conditional Stages

使用Fluent聚合接口时,为什么后面添加的stages(stage 3)不起作用?

var fluentPipeline = _userToGroup.Aggregate()
            .AppendStage<BsonDocument>("stage 1")
            .AppendStage<BsonDocument>("stage 2");
if (condition)
    fluentPipeline.AppendStage<BsonDocument>("stage 3");
fluentPipeline.ToListAsync();

第 3 阶段在添加到同一代码行中的管道时工作,如下所示。这意味着这不是舞台的问题,而是据我了解如何将舞台添加到管道中。问题是为什么?

var fluentPipeline = _userToGroup.Aggregate()
            .AppendStage<BsonDocument>("stage 1")
            .AppendStage<BsonDocument>("stage 2")
            .AppendStage<BsonDocument>("stage 3");

您可以打开github预览AppendStage方法的主体。

原来有一个 return 语句所以它总是 returns 一个新的 PipelineDefinition 而不是修改现有的。因此,您必须将此返回值分配给一个变量。尝试:

var fluentPipeline = _userToGroup.Aggregate()
            .AppendStage<BsonDocument>("stage 1")
            .AppendStage<BsonDocument>("stage 2");

if (condition)
    fluentPipeline = fluentPipeline.AppendStage<BsonDocument>("stage 3");

fluentPipeline.ToListAsync();