Skip to content

Instantly share code, notes, and snippets.

@izquiratops
Last active June 2, 2020 09:40
Show Gist options
  • Save izquiratops/5a2dc3757745211ab6e33870fd23dd9e to your computer and use it in GitHub Desktop.
Save izquiratops/5a2dc3757745211ab6e33870fd23dd9e to your computer and use it in GitHub Desktop.
Date Aggregation Mongodb
{
  _id: {
    flightOperationHash: "$flightOperationHash",
    interval: {
      $subtract: [
        { "$minute": "$time" },
        { "$mod": [{ "$minute": "$time"}, 1] }
      ]
    }
  },
  grouped_data: { $push: {timestamp: "$timestamp"} },
  count: { $sum: 1 }
}

Found from StackOverflow

db.collection.aggregate([
    {
        "$project": {
            "date": { "$add": [new Date(0), "$timestamp"] },
            "timestamp": 1,
            "value": 1
        }
    },
    { 
        "$group": {
            "_id": {
                "year": { "$year": "$date" },
                "dayOfYear": { "$dayOfYear": "$date" },
                "interval": {
                    "$subtract": [ 
                        { "$minute": "$date" },
                        { "$mod": [{ "$minute": "$date"}, 10 ] }
                    ]
                }
            },
            "grouped_data": { "$push": {"timestamp": "$timestamp", "value": "$value" } }
        }
    },
    {
        "$project":{
            "_id": 0,
            "grouped_data": 1
        }
    }
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment