Two Hours Mongodb Aggregation
<p>Here is my sample Data:</p>

<pre><code>{
"_id": {
    "$oid": "5654a8f0d487dd1434571a6e"
},

"ValidationDate": {
    "$date": "2015-11-24T13:06:19.363Z"
},

"DataRaw": " WL 00100100012015-08-28 02:44:17+0000+ 16.81 8.879  1084.00",

"ReadingsAreValid": true,

"locationID": " WL 001",

"Readings": {

    "pH": {
        "value": 8.879
    },

    "SensoreDate": {
        "value": {
            "$date": "2015-08-28T02:44:17.000Z"
        }
    },

    "temperature": {
        "value": 16.81
    },

    "Conductivity": {
        "value": 1084
    }
},
"HMAC":"ecb98d73fcb34ce2c5bbcc9c1265c8ca939f639d791a1de0f6275e2d0d71a801"
</code></pre>

<p>}</p>

<p>I am trying to group average values by two hours interval and have the following aggregation query.</p>

<pre><code>Query = [{"$unwind":"$Readings"},                    
        {'$group' : { "_id": {
            "year": { "$year": "$Readings.SensoreDate.value" },
            "dayOfYear": { "$dayOfYear": "$Readings.SensoreDate.value" },
            "interval": {
                "$subtract": [ 
                    { "$hour": "$Readings.SensoreDate.value"},
                    { "$mod": [{ "$hour": "$Readings.SensoreDate.value"},2]}
                ]
            }
        }},
'AverageTemp' : { '$avg' : '$Readings.temperature.value'}, "AveragePH": {"$avg" : "$Readings.pH.value"}, "AverageConduc": {"$avg" : "$Readings.Conductivity.value"}}
, {"$limit":10}]
</code></pre>

<p>This gives me an error saying 
<code>A pipeline stage specification object must contain exactly one field.</code> and I have done all research but can't get the desired results.</p>

<p>After some formatting, your present aggregation pipeline looks like:</p>

<pre><code>Query = [
    { "$unwind": "$Readings" },                    
    {
        '$group' : { 
            "_id": {
                "year": { "$year": "$Readings.SensoreDate.value" },
                "dayOfYear": { "$dayOfYear": "$Readings.SensoreDate.value" },
                "interval": {
                    "$subtract": [ 
                        { "$hour": "$Readings.SensoreDate.value"},
                        { 
                            "$mod": [
                                { "$hour": "$Readings.SensoreDate.value" },
                                2
                            ]
                        }
                    ]
                }
            }
        },
        'AverageTemp' : { '$avg' : '$Readings.temperature.value' }, 
        "AveragePH": { "$avg" : "$Readings.pH.value" }, 
        "AverageConduc": { "$avg" : "$Readings.Conductivity.value" }
    }, 
    { "$limit": 10 }
]
</code></pre>

<p>with which mongo is complaining </p>

<blockquote>
  <p>A pipeline stage specification object must contain exactly one field.</p>
</blockquote>

<p>because it's failing to recognise the misplaced fields</p>

<pre><code>'AverageTemp' : { '$avg' : '$Readings.temperature.value' }, 
"AveragePH": { "$avg" : "$Readings.pH.value" }, 
"AverageConduc": { "$avg" : "$Readings.Conductivity.value" }
</code></pre>

<p>A correct pipeline should have these fields within the <strong><code>$group</code></strong> pipeline stage, so a working pipeline follows:</p>

<pre><code>Query = [
    { "$unwind": "$Readings" },                    
    {
        "$group" : { 
            "_id": {
                "year": { "$year": "$Readings.SensoreDate.value" },
                "dayOfYear": { "$dayOfYear": "$Readings.SensoreDate.value" },
                "interval": {
                    "$subtract": [ 
                        { "$hour": "$Readings.SensoreDate.value"},
                        { 
                            "$mod": [
                                { "$hour": "$Readings.SensoreDate.value" },
                                2
                            ]
                        }
                    ]
                }
            },
            "AverageTemp" : { "$avg" : "$Readings.temperature.value" }, 
            "AveragePH": { "$avg" : "$Readings.pH.value" }, 
            "AverageConduc": { "$avg" : "$Readings.Conductivity.value" }
        }       
    }, 
    { "$limit": 10 }
]
</code></pre>

javascript python arrays mongodb pymongo-3.x