我需要帮助将一个MongoDB查询转换为C#查询。 这是我的模式:
public class Planner
{
public ObjectId Id { get; set; }
public string PlannerName { get; set; }
public string EditorName { get; set; }
public DateTime DateModified { get; set; }
public List<PlannerDayModel> PlannerDays { get; set; }
}
public class PlannerDayModel
{
public int WeekDay { get; set; }
public string WeekDayStr { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public int MonthDay { get; set; }
public DateTime TimeStamp { get; set; }
public bool IsWeekend { get; set; }
public bool IsHoliday { get; set; }
public PlannerFreeTextModel FreeTextBox1 { get; set; }
public PlannerFreeTextModel FreeTextBox2 { get; set; }
public List<PlannerEventModel> Events { get; set; }
}
public class PlannerEventModel
{
public int EventRowId { get; set; }
public string EventName { get; set; }
public bool IsSeries { get; set; }
public int? Episode { get; set; }
public int? Season { get; set; }
public bool? IsCustom{ get; set; }
}
public class PlannerFreeTextModel
{
public int EventRowId { get; set; }
public string Text { get; set; }
}
我正在尝试获取所有计划程序文档,其中一个名为Events
的嵌套集合具有事件名称为x
的事件。
这个MongoDB查询很好用:
db.Planner.aggregate([
{ "$match": { "planners.events.eventName": "X" } },
{ "$project":
{"dateModified":1, "editorName":1,"plannerName":1,
"planners": {
"$filter": {
"input": {
"$map": {
"input": "$planners",
"as": "planner",
"in": {
"weekDay": "$$planner.weekDay",
"weekDayStr":"$$planner.weekDay",
"events": {
"$filter": {
"input": "$$planner.events",
"as": "event",
"cond": {
"$eq": ["$$event.eventName", "X" ]
}
}
}
}
}
},
"as": "planner",
"cond": { "$ne": [ "$$planner.events", []]}
},
}
}}
])
但我无法找到等效的C#驱动程序查询来工作。
我尝试了这个,但它抛出了错误“无法确定表达式的序列化信息”。
var projection = Builders<Planner>.Projection
.Include(x => x.PlannerDays
.Find(p => p.Events.FirstOrDefault(e => e.EventName == eventName) != null))
.Include(x => x.DateModified)
.Include(x => x.EditorName)
.Include(x => x.Id)
.Include(x => x.PlannerName);
var res = collection
.Aggregate()
.Match(Builders<Planner>.Filter.Eq("planners.events.eventName", eventName))
.Project<Planner>(projection)
.ToList();