How to query date range on the MongoDB collection where the ISO date is stored in string field?

21,131

You can make the queries exactly as you did in the example.

The string ordering is consistent and will give you the exact relationship you want.

Share:
21,131
Amol M Kulkarni
Author by

Amol M Kulkarni

Fell in ♥ with Codes... Especially JS ;-) Currently Working on broader web platform (targeting all possible devices and browsers)You'll find me contributing, authoring many projects across the internet. I also ♥ helping others in solving technical problem & I believe this is a way to stay up-to-date and give something back to the community from where I have learnt and still learning :) Worked on: ADO.NET, AJAX, ASP.NET, C, C#, C++, Corel draw, CouchDB, CouchBase, Crystal Reports, CSS, DHTML, Dreamweaver, EJS, Express Framework Node.js, Go-lang, HTML, Infragistics, Jade, Java, Java Applets, JavaScript, jQuery, JSP, Membase, Memcache, Microsoft SQL Server, MongoDB with Python and Node.js, MongoDB(DBA), MongoDB (4.2-Basics), MongoDB New Features and Tools in 4.2, MongoDB Performance Tuning, MS-DOS, Netbeans, No SQL, Node.js, Oracle, OAuth, PageMaker, PhotoShop, Servlets, Socket.io, SwishMax, Tally, VBA, VBScript, Visual Basic, Visual InterDev, Windows Mobile C#, XHTML, XML, XQuery, XSLT, Python, PHP, Kafka, Storm, 0MQ (ZMQ), Redis, Hadoop Favorite Quotation: "Only He Who Can See The Invisible Can Do The Impossible." profile for Amol M Kulkarni on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/2185406.png

Updated on July 14, 2022

Comments

  • Amol M Kulkarni
    Amol M Kulkarni almost 2 years

    Scenario: Consider I am having a collection called MyCollection, with following data:

    {
        "_id" : 'MyUniqueID_01'
        "CreatedTime" : "2013-12-01T14:35:00Z",
        "LastModifiedTime" : "2013-12-01T13:25:00Z"
    }
    

    Now I want to query the MongoDB database where the above mentioned kind of data is in huge number of documents. And my query is based on date range i.e. using $gt, $gte, $lt & $lte

    So my query may be something like:

    db.MyCollection.find({ 'CreatedTime': {$gt: '2013-05-25T09:29:40.572Z'}})
    

    Considering the above examples the expected result is, query has to get a document (since the "CreatedTime" : "2013-12-01T14:35:00Z" is greater than value passed in query '2013-05-25T09:29:40.572Z'); whereas it not, the issue is that field CreatedTime is in string format.

    Question: Is there any way so that I can get my expected result perfectly without changing the string field type to date?

  • Prasad Silva
    Prasad Silva over 8 years
    Yes, but I would also add that the query string must use the same timezone suffix as the data set (which also adds another constraint on how you're storing the data strings) because you're doing a string comparison. For example, the OP's example is in ISO 8601 UTC form (with the 'Z' suffix), therefore the query string must also be in that form.