小编Sha*_*tan的帖子

如何在 C# 中使用管道和 let 参数进行 $lookup (MongoDB.Driver 2.7.2)

我需要在我的 C# 代码中在 MongoDB 中运行以下查询。我使用 MongoDB.Driver 2.7.2 和 MongoDB 4.0。

我想使用 $lookup 而不是 $project / $unwind 以防止命名集合的每个可能字段。

db.getCollection('Collection1').aggregate([  
    { "$match" : { "Id" : 1 } },
        { "$lookup": {
            "from": "Collection2",
            "let": { collection1Id : "$Id" },
            "pipeline": [
                { $match:
                    { $expr:
                        { $and:
                            [
                                { $eq : [ "$Collection1Id",  "$$collection2Id" ] },
                                { $in : [ "$_id" , [1, 2, 3]] }
                            ]
                            }
                        }
                    }
            ],
            "as": "item"
        }
    } 
])
Run Code Online (Sandbox Code Playgroud)

我期望在多个连接条件下 Collection1 与 Collection2 连接的输出。

c# mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
4246
查看次数

标签 统计

c# ×1

mongodb ×1

mongodb-.net-driver ×1