使用mongodb从列表中查询对象

时间:2014-11-21 05:30:53

标签: mongodb

如何查询只返回房间的学生:102岁,年龄> = 18? 我希望获得如下输出:

    {
        "name": "john",
        "age": 21,
        "books": [
               {
                "title": "algebra",
                "pages": 300,
                "author": "Jkrowling"
               }
           ]
        }

并提供以下文件

{
"teacher": "teachername",
"room": 102,
"students": [
        {
        "name": "john",
        "age": 21,
        "books": [
               {
                "title": "algebra",
                "pages": 300,
                "author": "Jkrowling"
               }
           ]
        }
    ]
}    

1 个答案:

答案 0 :(得分:0)

以下聚合将有助于

db.collectionName.aggregate({"$unwind":"$students"},
     {"$match":{"room":102,"students.age":{"$gt":18}}}).pretty()