r mongolite - date query

后端 未结 4 1438
执念已碎
执念已碎 2021-01-23 01:59

Question

Using the mongolite package in R, how do you query a database for a given date?

Example Data

4条回答
  •  后悔当初
    2021-01-23 02:36

    as this question keeps showing up at the top of my google results when i forget AGAIN how to query dates in mongolite and am too lazy to go find the documentation:

    the above Mongodb shell query,

    db.test.find({"dte" : {"$gt" : new ISODate("2015-01-01")}})

    now translates to

    mong$find('{"dte":{"$gt":{"$date":"2015-01-01T00:00:00Z"}}}')

    optionally, you can add millis:

    mong$find('{"dte":{"$gt":{"$date":"2015-01-01T00:00:00.000Z"}}}')

    if you use the wrong datetime format, you get a helpful error message pointing you to the correct format: use ISO8601 format yyyy-mm-ddThh:mm plus timezone, either "Z" or like "+0500"

    of course, this is also documented in the mongolite manual

提交回复
热议问题