1

This my collection:

{
    "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
                } 

            ]
        } 

    ]
}

This was i tried:

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

In my collection i need to get first set off Data array fields only,based on filter condition variables array controller,tType fields, but in my above aggregate query it retrieve all variables arrays

Cœur
  • 37,241
  • 25
  • 195
  • 267
venkat
  • 69
  • 2
  • 9

1 Answers1

0

You can try this query:

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()
nurulnabi
  • 459
  • 3
  • 11