【发布时间】:2019-04-22 14:11:11
【问题描述】:
我正在尝试执行简单的展开并替换 .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 方法可以接收的可能参数之一,但我无法完全理解它。
任何帮助将不胜感激。
【问题讨论】:
标签: c# asp.net-core mongodb-query