Sanka
Sanka

Reputation: 1324

MongoDB : How to get the time difference in milliseconds between given 2 dates

I am expecting to get a time difference in milliseconds from 2 columns completedTime, createdTime. MongoDB : How to get the time difference between given 2 days responseTime = completedTime -createdTime

[
{
    "_id": {
    "$numberLong": "3804"
    },
    "hashcode": 1444331657,
    "token": "3804",
    "resultCount": 4,
    "createdTime": "2021-08-21T05:56:56.964Z",
    "isCompleted": true,
    "_class": "com.intervest.price.oxygen.model.ClientMatrixMongo",
    "completedTime": "2021-08-21T05:56:59.884Z"
},
{
    "_id": {
    "$numberLong": "3805"
    },
    "hashcode": 1444331657,
    "token": "3805",
    "resultCount": 4,
    "createdTime": "2021-08-21T05:56:57.964Z",
    "isCompleted": true,
    "_class": "com.intervest.price.oxygen.model.ClientMatrixMongo",
    "completedTime": "2021-08-21T05:56:59.884Z"
}
]

Code I tried

db.collection.aggregate([
    {
        "$match": {
            "token": "3805"
        }
    },
    {
        $addFields: {
            loginTime: {
                $dateFromString: {
                    dateString: "$createdTime"
                }
            },
            logoutTime: {
                $dateFromString: {
                    dateString: "$completedTime"
                }
            }
        }
    },
    {
        $addFields: {
            difference: {
                $subtract: [
                    "$logoutTime",
                    "$loginTime"
                ]
            }
        }
    },
    {
        "$project": {
            "difference": 1,
            "token": 1
        }
    }
])

result

[
    {
        "_id": NumberLong(3805),
        "difference": NumberLong(1920),
        "token": "3805"
    }
]

But when the document id like this how the query should be ?

[
    {
        "_id": {
            "$numberLong": "3804"
        },
        "hashcode": 1444331657,
        "token": "3804",
        "resultCount": 4,
        "createdTime": {
            "$date": "2021-08-21T05:56:57.964Z"
        },
        "isCompleted": true,
        "_class": "com.intervest.price.oxygen.model.ClientMatrixMongo",
        "completedTime": {
            "$date": "2021-08-21T05:56:59.884Z"
        }
    },
    {
        "_id": {
            "$numberLong": "3805"
        },
        "hashcode": 1444331657,
        "token": "3804",
        "resultCount": 4,
        "createdTime": {
            "$date": "2021-08-21T05:56:57.964Z"
        },
        "isCompleted": true,
        "_class": "com.intervest.price.oxygen.model.ClientMatrixMongo",
        "completedTime": {
            "$date": "2021-08-21T05:56:59.884Z"
        }
    }
]

Upvotes: 1

Views: 405

Answers (1)

YuTing
YuTing

Reputation: 6629

The code you tried has solved most of the problems.

Try below

db.collection.aggregate([
    {
        "$match": {
            "token": "3804"
        }
    },
    {
        $addFields: {
            difference: {
                $subtract: [
                    "$completedTime",
                    "$createdTime"
                ]
            }
        }
    },
    {
        "$project": {
            "difference": 1,
            "token": 1
        }
    }
])

Result

/* 1 */
{
    "_id" : NumberLong(3804),
    "token" : "3804",
    "difference" : NumberLong(1920)
}

/* 2 */
{
    "_id" : NumberLong(3805),
    "token" : "3804",
    "difference" : NumberLong(1920)
}

Upvotes: 2

Related Questions