I have a collection where a single document looks like this:
{
"_id" : "123456789",
"created_timestamp" : "1522950461031"
}
As you can see, the created_timestamp date is stored as the epoch value as a String. (unfortunately can't change that now)
I need to write a query that will calculate the "time elapsed" i.e, current time - created_time
for all of the documents at any given point in time.
This is what I tried so far:
db.getCollection('testEntity').aggregate([
{$match : {"_id" : "123456789"}},
{$project: {_id : 1, created_timestamp: 1 ,
dateDifference: { $subtract: [ new Date() , new Date("$created_timestamp")]}}}])
But it does not compute the difference correctly. I think the field is not getting detected correctly within the $subtract
. Any suggestions to rewrite it correctly?