Find objects between two dates MongoDB

后端 未结 14 2047
余生分开走
余生分开走 2020-11-21 06:03

I\'ve been playing around storing tweets inside mongodb, each object looks like this:

{
\"_id\" : ObjectId(\"4c02c58de500fe1be1000005\"),
\"contributors\" :          


        
14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 06:08

    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 (here i am using mongoose module and want results for rows whose date attribute is less than (before) the date given as myDate param) can handle it correctly:

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

提交回复
热议问题