Query the string type Date in mongodb

前端 未结 3 722
醉梦人生
醉梦人生 2021-01-14 05:07

This is my query in mongodb

db.order.find ({\"ublExtensions.ublExtensions.extensionContent.Metadata
                .ModificationTime\": \"2012-02-04T01:58:2         


        
3条回答
  •  悲&欢浪女
    2021-01-14 05:17

    I would recommend storing your dates as Date or ISODate on MongoDB since it would ease your life :)

    If not possible, then on the client side (programming language specific driver) you should be able to parse the string to the appropiate Date type.

    However, if you need your query to run on mongo shell, you could follow an strategy similar as described at Mongo Cookbook

    1) Creating a var with today's date and yesterday's date:

    >var today = new Date();
    >today
    ISODate("2012-11-26T22:12:03.870Z")
    >var yesterday = new Date()
    >yesterday.setDate(today.getDate() - 1)
    >yesterday
    ISODate("2012-11-25T22:12:03.870Z")
    

    2) Form a query for finding documents within a 24 hours period

    >db.order.find( { "ublExtensions.ublExtensions.extensionContent.Metadata.ModificationTime" : { $gte : yesterday, $lt : today } } )
    

提交回复
热议问题