我有这些类:在阵列结合了父文档的属性值和ElemMatch
public class Parent
{
public int Id { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public ChildInfo ChildInfo { get; set; }
}
public class ChildInfo
{
public int Age { get; set; }
...other properties...
}
所以给家长的集合,我需要用一个特定ID返回父母,但前提是它没有具有特定年龄的ChildInfo的孩子。
我觉得我很接近。以下是我迄今为止:
var childQuery = Query<Child>.NE(c => c.ChildInfo.Age, 5);
var finalresult = collection.Find(Query.And(Query<Parent>.EQ(p => p.Id, 3245),
Query<Parent>.ElemMatch(p => p.Children, builder => childQuery)));
不过,我得到这些结果:
如果没有父母3245,无返回值(正确的)。
当父3245没有孩子时,查询返回什么(错误)。
当父3245有一个小孩3岁时,查询返回父(正确)。
当父母3245有一个孩子3岁和一个孩子5岁时,查询返回父母(错误)。
当父母3245有一个孩子3岁和一个孩子7岁时,查询返回父母(正确)。
它看起来好像查询的第一部分(Parent.Id)起作用。但是下半场似乎总是返回父节点,除非列表为空。