{
"_id" : ObjectId("50ed90a55502684f440001ac"),
"time" : "2020-05-14T14:12:46.058-0400",
"ServerTimeStamp" : "2020-05-14T14:12:46.058-0400"
}
{
"_id" : ObjectId("50ed90a55502684f440001advert"),
"time" : "2020-05-14T15:12:46.058-0400",
"ServerTimeStamp" : "2020-05-14T14:12:46.058-0400"
}
{
"_id" : ObjectId("50ed90a55502684f440001ae"),
"time" : "2020-05-14T18:12:46.058-0400",
"ServerTimeStamp" :"2020-05-14T14:12:46.058-0400"
}
I wish to calculate the time distinction of above rows ( in seconds) as beneath . right here the time discipline is in string format as an alternative of datetime
{
"_id" : ObjectId("50ed90a55502684f440001ac"),
"time" : "2020-05-14T14:12:46.058-0400",
"time_difference" :null
}
{
"_id" : ObjectId("50ed90a55502684f440001advert"),
"time" : "2020-05-14T15:12:46.058-0400",
"time_difference" : 3600
}
{
"_id" : ObjectId("50ed90a55502684f440001ae"),
"time" : "2020-05-14T18:12:46.058-0400",
"time_difference" : 10800
}
I attempted the beneath question however getting error and i’ve already transformed the time thus far from string
db.hey.combination(
{$kind: {$dateFromString:{time: 1}}},
{$group: {_id: 0, doc: {$push: '$$ROOT'}}},
{$venture: {documentAndPrevTime: {$zip: {inputs: ['$document', {$concatArrays: [[null], '$doc.time']}]}}}},
{$unwind: {path: '$documentAndPrevTime'}},
{$replaceWith: {$mergeObjects: [{$arrayElemAt: ['$documentAndPrevTime', 0]}, {prevTime: {$arrayElemAt: ['$documentAndPrevTime', 1]}}]}},
{$set: {time_difference: {$trunc: [{$divide: [{$subtract: ['$time', '$prevTime']}, 1000]}]}}},
{$unset: 'prevTime'}
);