i am working on aggregation of mongodb collection.my mongodb collection has creation_time in timestamp.How will i can collect same day result and aggregate with next day res
In my case , I used
db.collection.aggregate([
{ "$project": {
"date": { "$toDate": {
$multiply:["$_id",1000 ]
}
}
}}
])
convert timestamp from milliseconds to seconds.
You can use $toDate aggregation to convert timestamp to ISO date and $toLong to convert string timestamp to integer value in mongodb 3.6
db.collection.aggregate([
{ "$project": {
"_id": {
"$toDate": {
"$toLong": "$_id"
}
}
}},
{ "$group": {
"_id": { "$dateToString": { "format": "%Y-%m-%d", "date": "$_id" } },
"count": { "$sum": 1 }
}}
])
Try it here
And with the previous versions
db.collection.aggregate([
{ "$project": {
"date": { "$add": [ new Date(0), "$_id" ] }
}}
])