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

4

我正在使用以下查询查找所有所需文件,然后应用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查询与现有的查找查询合并。
有什么想法可以通过查找实现吗?
2个回答

2
为了避免运行[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();

0
非常感谢。它按预期工作。最终的工作代码如下:
 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();

1
你可以像这样消除所有的BSONDocument噪音:https://dev.to/djnitehawk/overcoming-the-limitations-of-mongodb-c-driver-1110 - Dĵ ΝιΓΞΗΛψΚ
1
谢谢分享这个。让我来检查一下。 - Shilpa Bharkhada

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接