MongoDB嵌套ElemMatch和All

时间:2015-03-27 03:48:31

标签: mongodb

我想找一份同时有3岁儿童和4岁儿童的父母名单。

我的查询似乎有效:

db.SubArrays.find({
    Children: {$all: 
        [
               { "$elemMatch": { Age: 3}},
               { "$elemMatch": { Age: 4}}            
        ]
    }
})

我似乎无法将其转换为c#

中的查询

鉴于此数据:

/* 0 */
{
    "_id" : ObjectId("5514c620923a9b55e22f0adf"),
    "Name" : "Bob",
    "Children" : [ 
        {
            "Name" : "Kid1",
            "Age" : 5
        }, 
        {
            "Name" : "Kid2",
            "Age" : 4
        }, 
        {
            "Name" : "Kid3",
            "Age" : 4
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("5514c655923a9b55e22f0ae0"),
    "Name" : "Steve",
    "Children" : [ 
        {
            "Name" : "Kid4",
            "Age" : 3
        }, 
        {
            "Name" : "Kid5",
            "Age" : 4
        }, 
        {
            "Name" : "Kid6",
            "Age" : 4
        }
    ]
}

应该返回

{
    "_id" : ObjectId("5514c655923a9b55e22f0ae0"),
    "Name" : "Steve",
    "Children" : [ 
        {
            "Name" : "Kid4",
            "Age" : 3
        }, 
        {
            "Name" : "Kid5",
            "Age" : 4
        }, 
        {
            "Name" : "Kid6",
            "Age" : 4
        }
    ]
}

我已经搞砸了这段代码,但我似乎无法找出Query.All()的第二个参数

    var ages = new List<int>();
    ages.Add(4);
    ages.Add(3);


    var threeQuery = Query<Child>.EQ(x => x.Age, 3);
    var fourQuery = Query<Child>.EQ(x => x.Age, 4);

    var child3Query = Query<Parent>.ElemMatch(x => x.Children, b => threeQuery);
    var child4Query = Query<Parent>.ElemMatch(x => x.Children, b => fourQuery);
    Query<Parent>.All(x => x.Children);

0 个答案:

没有答案