如何使用 MongoDB C# 驱动程序聚合 $lookup?

How to aggregate $lookup with MongoDB C# driver?

我正在使用下面的查找来获取所有必需的文档,然后应用 LINQ 查询来查找特定文档,它按预期工作正常。

     new BsonDocument("$lookup", new BsonDocument()
                        .Add("from", "JournalInstructionReplication")
                        .Add("localField", "_id")
                        .Add("foreignField", "_eid")
                        .Add("as", "Replicated"))
 var cursor = await collection.AggregateAsync(pipeline, options);
            List<BsonDocument> list = cursor.ToList();

           var failedDocs = list.Where(d => d["Replicated"][0]["lastReplicationStatus"] != "success" ||
                             d["Replicated"][0]["eSeq"] != d["Replicated"][0]["lastReplicationSequence"])
                            .ToList();

我想将以上 LINQ 查询与现有查找查询合并。

知道如何通过查找实现吗?

为了避免 运行ning [0] 你可以 运行 $unwind 因为加入的集合之间显然有 1:1 关系(你通过 _id).内存中的部分必须转换为以下 $match:

{
    $match: {
        $expr: {
            $or: [
                { $ne: [ "$Replicated.lastReplicationStatus", "success" ] },
                { $ne: [ "$Replicated.eSeq", "$Replicated.lastReplicationSequence" ] },
            ]
        }
    }
}

在 C# 中如下所示:

var q = _channels.Aggregate()
                    .Lookup(
                    foreignCollectionName: "JournalInstructionReplication", 
                    localField: "_id", 
                    foreignField:"_id", 
                    @as: "Replicated")
    .Unwind("Replicated")
    .Match(new BsonDocument()
    {
        { "$expr", new BsonDocument()
        {
            { "$or", new BsonArray()
            {
                new BsonDocument(){{ "$ne", new BsonArray(){ "$Replicated.lastReplicationStatus", "success" } }},
                new BsonDocument(){{ "$ne", new BsonArray(){ "$Replicated.eSeq", "$Replicated.lastReplicationSequence" } }
            } }
        } }
    }});

var result = q.ToList();

非常感谢。它作为 expected.Final 工作代码工作如下:

 new BsonDocument("$lookup", new BsonDocument()
                        .Add("from", "JournalInstructionReplication")
                        .Add("localField", "_id")
                        .Add("foreignField", "_eid")
                        .Add("as", "Replicated")),
                 new BsonDocument("$unwind", new BsonDocument()
                        .Add("path", "$Replicated")),
                   new BsonDocument("$match", new BsonDocument()
                        .Add("$expr", new BsonDocument()
                                .Add("$or", new BsonArray()
                                        .Add(new BsonDocument()
                                                .Add("$ne", new BsonArray()
                                                        .Add("$Replicated.lastReplicationStatus")
                                                        .Add("success")
                                                )
                                        )
                                        .Add(new BsonDocument()
                                                .Add("$ne", new BsonArray()
                                                        .Add("$Replicated.eSeq")
                                                        .Add("$Replicated.lastReplicationSequence")
                                                )
                                        )
                                )
                        ))
            };

            var cursor = await collection.AggregateAsync(pipeline, options);
            List<BsonDocument> list = cursor.ToList();