1

In mongodb I have a date field with format like this "accessedAt" : "04-25-2018T21:12:53"

And I am not sure how to query it, I tried

db.collection.find({"accessedAt": {"$eq": new Date("04-25-2018T21:12:53")}})

db.collection.find({"accessedAt": {"$eq": ISODate("04-25-2018T21:12:53")}}).

Want to retrieve the all the documents that equals today date. Could someone please help me?

Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
rviDesai
  • 43
  • 2
  • 9
  • Try this: https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb – rodrigoap Jun 28 '19 at 04:25
  • It's strange to me. I wonder which data type of the 'accessedAt' field. Could you make a query to show an object information like this: { "_id" : ObjectId("5d15998a93cd5c2f578dcd9f"), "date1" : ISODate("2019-06-28T04:37:30.636Z"), "date2" : "Fri Jun 28 2019 11:37:30 GMT+0700 (SE Asia Standard Time)" } – Tuan Nguyen Jun 28 '19 at 04:41
  • Can you please post the answer how to find using var in the script. – rviDesai Jun 28 '19 at 04:54

1 Answers1

1

try this way :

db.collection.find({"accessedAt": {"$eq": ISODate("2018-04-25T21:12:53Z")}});

read more here :https://docs.mongodb.com/manual/reference/method/Date/

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • The ISO date didn't work Saurabh. And in that link I dont see it how to use it in find query. – rviDesai Jun 28 '19 at 03:57
  • @rviDesai , what you want get , describe in question , Do you want to find record by exact "accessedAt" datetime , then use "$eq" , else use "$gt" to find datetime grater than perticuler datetime , also you can use $lt , $lte , $gt, $gte operators – Saurabh Mistry Jun 28 '19 at 04:25