SAK
SAK

Reputation: 25418

How to filter mongo db documents using timespan with respect to Epoch time?

I have a document format as follows in MongoDB. The UpdatedOn is Epoch time. Is there a way I can filter the documents in my collection so that I get only the docs updated within the past hour?

{
    "_id" : ObjectId("5c29f3123d8cf714fd9cdb87"),
    "Machine" : "machine1",
    "Pools" : [
        "Pool1",
        "Pool2"
    ],
    "UpdatedOn": 1546330204
}

Upvotes: 1

Views: 467

Answers (1)

mickl
mickl

Reputation: 49945

You need to calculate the Epoch time on a client side, try in JS / Mongo shell:

db.col.find({ UpdatedOn: { $gt: new Date().valueOf() - 1000 * 60 * 60 } })

valueOf returns the number of millisecond since midnight January 1, 1970 UTC

Upvotes: 1

Related Questions