将根目录替换为.net core AggregateExpressionDefinition的C#驱动程序
我正在尝试在.NETCore2.2中执行一个简单的展开和替换root。 我已经在MongoDB中尝试过这个查询,它可以工作,但我发现如果不使用神奇的字符串,很难将它100%转换为C 这是我的文件:将根目录替换为.net core AggregateExpressionDefinition的C#驱动程序,c#,asp.net-core,mongodb-query,C#,Asp.net Core,Mongodb Query,我正在尝试在.NETCore2.2中执行一个简单的展开和替换root。 我已经在MongoDB中尝试过这个查询,它可以工作,但我发现如果不使用神奇的字符串,很难将它100%转换为C 这是我的文件: { "_id" : ObjectId("5cb6475b20b49a5cec99eb89"), "name" : "Route A" "isActive" : true, "buses" : [ { "capacity" : "
{
"_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中的这个查询得到的#
\u routes.Aggregate()
.Match(r=>r.Id==routeId)
.展开(r=>r.总线)
.ReplaceRoot($bus)
.ToListAsync();
我想知道是否有可能用非硬编码的字符串替换“$bus”
我尝试过使用AggregateExpressionDefinition类,它是ReplaceRoot方法可以接收的可能参数之一,但我无法完全理解它
任何帮助都将不胜感激。我可以为您提供一个使用MongoDAL的解决方案,它只是c#驱动程序的包装,隐藏了驱动程序的大部分复杂性
using System;
using System.Linq;
using MongoDAL;
namespace BusRoutes
{
class Route : Entity
{
public string name { get; set; }
public bool isActive { get; set; }
public Bus[] buses { get; set; }
}
class Bus
{
public int capacity { get; set; }
public string time { get; set; }
public string direction { get; set; }
}
class Program
{
static void Main(string[] args)
{
new DB("busroutes");
var routeA = new Route
{
name = "Route A",
buses = new Bus[]
{
new Bus { capacity = 15, direction = "Inbound", time = "8:00:00"},
new Bus { capacity = 25, direction = "Outbound", time = "9:00:00" },
new Bus { capacity = 35, direction = "Inbound", time = "10:00:00" }
}
};
routeA.Save();
var query = routeA.Collection()
.Where(r => r.ID == routeA.ID)
.SelectMany(r => r.buses);
Console.WriteLine(query.ToString());
var busesOfRouteA = query.ToArray();
foreach (var bus in busesOfRouteA)
{
Console.WriteLine(bus.time.ToString());
}
Console.ReadKey();
}
}
}
如果有人最终犯了与我相同的错误,请将此贴在此处 我基本上创建了一个新的“UnoundRoute”实体来保存展开操作的结果,然后使用了一个简单的LINQ表达式。感谢reddit用户u/Nugsly提出的关于更改我应该调用unwind的方式的建议 这项工作:
_routes.Aggregate()
.Match(r => r.Id == routeId)
.Unwind<Route, UnwoundRoute>(r => r.Buses)
.ReplaceRoot(ur => ur.Buses)
.ToListAsync();
此外,如果尝试添加结果类型以替换根VS,则会抛出一个错误,说明lambda表达式无法转换,因为它不是委托类型
这不是(这是我在乞讨中遇到的):
\u routes.Aggregate()
.Match(r=>r.Id==routeId)
.展开(r=>r.总线)
.ReplaceRoot(ur=>ur.Bus)
.ToListAsync();
_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"
}
_routes.Aggregate()
.Match(r => r.Id == routeId)
.Unwind<Route, UnwoundRoute>(r => r.Buses)
.ReplaceRoot<Bus>(ur => ur.Buses)
.ToListAsync();