How do I fetch all mongo data which is created gre

2019-09-10 21:31发布

I read the $gt attribute has to be used. Not able to get around this. Let's say I have some mongo data like this:

{
    "startTime" : "Sun 25 Jan 2015 07:14:26 GMT",
    "endTime" : "",
    "jobStatus" : "JOBCANCELLED",
    "uiState" : "HISTORY",
    "priority" : "SILVER"
}

That's how my start time is saved in my Mongo. If I want to get the statuses of all jobs which have the start time greater than today, How do I do it?

db.getCollection('jobsCollection').find({"startTime":{$gt: "What here?"})

1条回答
We Are One
2楼-- · 2019-09-10 22:07

First you need to convert your startTime from String format to date time format.

To do from mongo shell :

 db.jobsCollection.find().forEach(function(doc){doc.startTime = new Date(doc.startTime);db.jobsCollection.save(doc)});

And then you can write the query for greater than date :

db.jobsCollection.find({"startTime":{$gt: new Date("2014-02-10")});

Let me know if you face any issue.

查看更多
登录 后发表回答