Post
Topic
Board Services
Topic OP
[CLOSED] ANSWER SIMPLE MONGODB QUESTION: GET 0.05 BITCOINS :) [CLOSED]
by
Lucky7Gaming
on 04/07/2016, 23:17:24 UTC
So my friend has stuff posting in a mongo database like this:
Code:
{
    "_id" : "device01",
    "event" : [
            {
                    "temperature" : "32.00",
                    "when" : ISODate("2016-07-02T00:21:41.441Z")
            },
            {
                    "temperature" : "42.00",
                    "when" : ISODate("2016-07-02T00:21:46.766Z")
            },            
    ]
}
 {
    "_id" : "device02",
    "event" : [
            {
                    "temperature" : "22.00",
                    "when" : ISODate("2016-06-02T00:21:41.441Z")
            },
            {
                    "temperature" : "43.00",
                    "when" : ISODate("2016-07-02T00:21:46.766Z")
            },            
    ]
}

and he wants to be able to query one of the devices on command and have an array return of the temperature, or when  (query device2 temp and get [22.00, 43.00] returned etc.  )

I tried this:
Code:
db.collection('test_mqtt').aggregate([
    {$unwind: '$event'},
    {$group: {
        _id: '$_id',
        t: {$push: '$event.temperature'}
        d: {$push: '$event.when'}
    }}
]);
but its somehow not working....its pulling my hair out, and driving me crazy.  Using Node, if anyone can figure it out, then send the code and post the address for 0.05 bitcoins!!!! Smiley