Inserting and Querying Date with MongoDB and Nodejs

后端 未结 4 919
春和景丽
春和景丽 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:55

    You need to pass a date object and not a date string.

    collection.findOne({last_updated: new Date('2014-01-22T14:56:59.301Z')},function(err, doc) {
    

    The MongoDB driver will transform it into ISODate:

    { 
       "_id" : ObjectId("52dfe0c469631792dba51770"), 
       "last_updated" : ISODate('2014-01-22T14:56:59.301Z') 
    }
    

    Check these questions:

    • MongoDB + nodejs : how to query ISODate fields?
    • ISODate is not defined

提交回复
热议问题