0

I would like to know how to query based on below condition in MongoDB. let's say I have a field in a collection called created_at and it is ISODate() now I want to get all the data like ISODate() + 1 Hour greater than current time. how do I do that ? I tried searching the web. I don't understand how to search.

{
    "_id" : ObjectId("55c48b37f09e091c1100002a"),
    "user_id" : 1,
    "flagged" : 0,
    "flag_count" : 0,
    "likes" : [ 
        1, 
        3, 
        7
    ],
    "dislikes" : [],
    "img_url" : "something :D",
    "place_id" : "55ba017ef09e098c1500002d",
    "updated_at" : ISODate("2015-08-28T09:30:44.000Z"),
    "created_at" : ISODate("2015-08-07T10:40:55.000Z"),
    "moderated" : true
}
styvane
  • 59,869
  • 19
  • 150
  • 156
Rooshan Akthar
  • 401
  • 5
  • 14
  • 4
    http://stackoverflow.com/questions/18233945/query-to-get-last-x-minutes-data-with-mongodb That would make db.[yourcollection].find({created_at: {$gt: new Date(ISODate().getTime() - 1000 * 3600 * 1)}}); – piscator Sep 17 '15 at 12:43
  • @piscator thank you :) it worked. there is an additional problem. I am running it in PHP. let me sort it out. – Rooshan Akthar Sep 17 '15 at 12:49

1 Answers1

0

Create a IsoDate with one hour less and use $gt

Felix Schmidt
  • 321
  • 1
  • 3
  • 13
  • No, It cannot be done because, 1 hour is not a constant variable. It can be changed to several hours. – Rooshan Akthar Sep 17 '15 at 12:10
  • db.collection.find( { created_at: { $gt: ISODate("2015-08-07T9:40:55.000Z")} } ) should match your document – Felix Schmidt Sep 17 '15 at 12:13
  • It was an example for structure. Let me explain more. a user post something on 24.09.2015 I put the interval as 24 hours. now it has to filtered in my query if it is 24 hours older. – Rooshan Akthar Sep 17 '15 at 12:18
  • 2
    You have to create a Date object that has x hours less than the current date. And then put it into you query. – Felix Schmidt Sep 17 '15 at 12:23