c# - 将 Root 替换为 .net core AggregateExpressionDefinition 的 C# 驱动程序

标签 c# asp.net-core mongodb-query

我正在尝试在 .net core 2.2 中执行简单的展开并替换 root。 我已经在 MongoDB 中尝试过查询并且它有效,但我发现如果不使用魔术字符串,很难将其 100% 转换为 C#。

这是我的文档:

{
    "_id" : ObjectId("5cb6475b20b49a5cec99eb89"),
    "name" : "Route A"
    "isActive" : true,
    "buses" : [ 
        {
            "capacity" : "15",
            "time" : "08:00:00",
            "direction" : "Inbound"
        },
        {
            "capacity" : "15",
            "time" : "08:30:00",
            "direction" : "Inbound"
        },
        {
            "capacity" : "15",
            "time" : "08:00:00",
            "direction" : "Outbound"
        },
        {
            "capacity" : "15",
            "time" : "08:30:00",
            "direction" : "Outbound"
        }
    ]
}

我还有一个名为 Routes 的根文档类和另一个名为 Bus 的子文档类。

我在 mongo 中运行的查询是这个:

db.routes.aggregate([
    { $match : { "_id" : ObjectId("5cb4e818cb95b3572c8f0f2c") } },
    { $unwind: "$buses" },
    { $replaceRoot: { "newRoot": "$buses" } }
])

预期的结果是一个简单的总线数组,到目前为止我在 C# 中通过这个查询得到它

_routes.Aggregate()
                .Match(r => r.Id == routeId)
                .Unwind<Route, UnwoundRoute>(r => r.Buses)
                .ReplaceRoot<Bus>("$buses")
                .ToListAsync();

我想知道是否可以用非硬编码的内容替换字符串“$buses”。

我已经尝试使用 AggregateExpressionDefinition 类,它是 ReplaceRoot 方法可以接收的可能参数之一,但我无法完全理解它。

我们将不胜感激。

最佳答案

将此张贴在这里以防有人最终犯下与我相同的错误。

我基本上创建了一个新的“UnwoundRoute”实体来保存展开操作的结果,然后使用了一个简单的 LINQ 表达式。感谢 reddit 用户 u/Nugsly 提出的关于改变我调用 unwind 的方式的建议。

这个有效:

_routes.Aggregate()
                .Match(r => r.Id == routeId)
                .Unwind<Route, UnwoundRoute>(r => r.Buses)
                .ReplaceRoot(ur => ur.Buses)
                .ToListAsync();

您还可以在之后过滤替换根的结果:

_routes.Aggregate()
                .Match(r => r.Id == routeId)
                .Unwind<Route, UnwoundRoute>(r => r.Buses)
                .ReplaceRoot(ur => ur.Buses)
                .Match(b => b.Direction == direction)
                .ToListAsync();

它会返回一个文档数组。

{
    "capacity" : "15",
    "time" : "08:00:00",
    "direction" : "Inbound"
},
{
    "capacity" : "15",
    "time" : "08:30:00",
    "direction" : "Inbound"
}

此外,如果您尝试添加结果类型来替换根 VS 将抛出一个错误,指出无法转换 lambda 表达式,因为它不是委托(delegate)类型。

这不是(这是我在开始时所拥有的):

_routes.Aggregate()
                .Match(r => r.Id == routeId)
                .Unwind<Route, UnwoundRoute>(r => r.Buses)
                .ReplaceRoot<Bus>(ur => ur.Buses)
                .ToListAsync();

关于c# - 将 Root 替换为 .net core AggregateExpressionDefinition 的 C# 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55795754/

相关文章:

c# - Web Api,如何从 HttpResponseMessage 返回枚举文本值而不是枚举索引值

c# - 在 C# 中重载复合赋值运算符的简单方法?

c# - 无法将 'Microsoft.NETCore.App (>= 2.0.0)' 解析为 '.NETCoreApp,Version=v2.0'

asp.net-core - dotnet运行具有特定URL的网站

mongodb - 使用 let 变量的管道和 geoIntersect 查找

java - Mongodb聚合命令转java代码

c# - 您如何在 Web 应用程序中集中 Entity Framework 数据上下文?

javascript - 检查参数的更好方法?

asp.net-core - Asp.Net Core 5 API Web 以 WCF 等流模式上传大文件

MongoDB 聚合 $or 与 $elemMatch、$expr 在 $lookup 管道内