How do I fetch all mongo data which is created greater than a particular date?

爱⌒轻易说出口 提交于 2019-12-12 02:44:56

问题


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:


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.



来源:https://stackoverflow.com/questions/33015162/how-do-i-fetch-all-mongo-data-which-is-created-greater-than-a-particular-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!