Mongodb检索文档中的数组字段

时间:2017-04-06 09:13:05

标签: mongodb aggregation-framework

这是我的收藏:

pageIndex

这是我试过的:

{
    "Id" : "4",
    "tType" : "77",
    "tId" : "001",
    "variables" : [ 
        {
            "controller" : 4444,
            "mod" : 41,
            "var" : "41",
            "Type" : 4,
            "vType" : "DI",
            "duration" : "",

            "Data" : [ 
                {
                    "Time" : 1483209000,
                    "value" : 50
                }, 
                {
                    "Time" : 1483209000,
                    "value" : 50
                }
            ]
        }, 
        {
            "controller" : 1,
            "mod" : 1,
            "var" : "1",
            "Type" : 11,
            "vType" : "AO",
            "duration" : "",

            "Data" : [ 
                {
                    "Time" : 1483209000,
                    "value" : 30
                }, 
                {
                    "Time" : 1483209000,
                    "value" : 30
                } 

            ]
        } 

    ]
}

在我的集合中,我需要首先设置数据数组字段,基于过滤条件变量数组控制器,tType字段,但在我上面的聚合查询中它检索所有变量数组

1 个答案:

答案 0 :(得分:0)

您可以尝试此查询:

db.app.aggregate([ {$match:{
"variables.controller":4444,
  "variables.mod":41,
  "variables.var":"41"
}}, {$project:{data:"$variables.Data", _id:0}},{$unwind:"$data"},{$unwind:"$data"},{$limit:1} ]).pretty()