Inserting and Querying Date with MongoDB and Nodejs

后端 未结 4 932
春和景丽
春和景丽 2020-11-29 00:55

I need some help finding a record by date in mongodb and nodejs.

I add the date to the json object in a scraping script as follows:

jsonObj.last_upda         


        
4条回答
  •  天命终不由人
    2020-11-29 01:48

    To clarify. What is important to know is that:

    • Yes, you have to pass a Javascript Date object.
    • Yes, it has to be ISODate friendly
    • Yes, from my experience getting this to work, you need to manipulate the date to ISO
    • Yes, working with dates is generally always a tedious process, and mongo is no exception

    Here is a working snippet of code, where we do a little bit of date manipulation to ensure Mongo can handle it correctly. In this example, I am using mongoose module and want results for rows whose date attribute is less than (ie. before) the date given as myDate param.

    var inputDate = new Date(myDate.toISOString());
    MyModel.find({
        'date': { $lte: inputDate }
    })
    

提交回复
热议问题