mongodb remove all dates less than specified

前端 未结 4 1148
孤独总比滥情好
孤独总比滥情好 2021-01-02 06:50

I have the following data.

{
    deviceID: 186,
    date: \"2014-3-15\"
}
{
    deviceID: 186,
    date: \"2014-3-14\"
}
{
    deviceID: 186,
    date: \"201         


        
4条回答
  •  我在风中等你
    2021-01-02 07:28

    If you want to remove data from MongoDB from the date less than specified, you MUST make sure of the date.

    Easiest way for you to check whether you are inputting the right format is to test it before you use it in your query.

    For example if you want to get current date in ISODate in Mongo shell, just type new Date and you will get the current date in Mongo.

    I've tried the following in the Mongo shell:

    new Date(2017, 11, 1)
    

    and it returns

    ISODate("2017-11-30T16:00:00Z")
    

    which is not what I wanted.

    What I want is to delete data before 1 November 2017.

    Here's what works for me:

    new Date("2017-11-01")
    

    and it returns:

    ISODate("2017-11-01T00:00:00Z")
    

    Which is what I wanted.

提交回复
热议问题