Find objects between two dates MongoDB

后端 未结 14 2078
余生分开走
余生分开走 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:05
    mongoose.model('ModelName').aggregate([
        {
            $match: {
                userId: mongoose.Types.ObjectId(userId)
            }
        },
        {
            $project: {
                dataList: {
                  $filter: {
                     input: "$dataList",
                     as: "item",
                     cond: { 
                        $and: [
                            {
                                $gte: [ "$$item.dateTime", new Date(`2017-01-01T00:00:00.000Z`) ]
                            },
                            {
                                $lte: [ "$$item.dateTime", new Date(`2019-12-01T00:00:00.000Z`) ]
                            },
                        ]
                     }
                  }
               }
            }
         }
    ])
    
    0 讨论(0)
  • 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 }
    })
    
    0 讨论(0)
  • 2020-11-21 06:09

    You can also check this out. If you are using this method, then use the parse function to get values from Mongo Database:

    db.getCollection('user').find({
        createdOn: {
            $gt: ISODate("2020-01-01T00:00:00.000Z"),
            $lt: ISODate("2020-03-01T00:00:00.000Z")
        }
    })
    
    0 讨论(0)
  • 2020-11-21 06:11

    Querying for a Date Range (Specific Month or Day) in the MongoDB Cookbook has a very good explanation on the matter, but below is something I tried out myself and it seems to work.

    items.save({
        name: "example",
        created_at: ISODate("2010-04-30T00:00:00.000Z")
    })
    items.find({
        created_at: {
            $gte: ISODate("2010-04-29T00:00:00.000Z"),
            $lt: ISODate("2010-05-01T00:00:00.000Z")
        }
    })
    => { "_id" : ObjectId("4c0791e2b9ec877893f3363b"), "name" : "example", "created_at" : "Sun May 30 2010 00:00:00 GMT+0300 (EEST)" }
    

    Based on my experiments you will need to serialize your dates into a format that MongoDB supports, because the following gave undesired search results.

    items.save({
        name: "example",
        created_at: "Sun May 30 18.49:00 +0000 2010"
    })
    items.find({
        created_at: {
            $gte:"Mon May 30 18:47:00 +0000 2015",
            $lt: "Sun May 30 20:40:36 +0000 2010"
        }
    })
    => { "_id" : ObjectId("4c079123b9ec877893f33638"), "name" : "example", "created_at" : "Sun May 30 18.49:00 +0000 2010" }
    

    In the second example no results were expected, but there was still one gotten. This is because a basic string comparison is done.

    0 讨论(0)
  • 2020-11-21 06:14
    db.collection.find({$and:
      [
        {date_time:{$gt:ISODate("2020-06-01T00:00:00.000Z")}},
         {date_time:{$lt:ISODate("2020-06-30T00:00:00.000Z")}}
       ]
     })
    
    ##In case you are making the query directly from your application ##
    
    db.collection.find({$and:
       [
         {date_time:{$gt:"2020-06-01T00:00:00.000Z"}},
         {date_time:{$lt:"2020-06-30T00:00:00.000Z"}}
      ]
    
     })
    
    0 讨论(0)
  • 2020-11-21 06:18

    Use this code to find the record between two dates using $gte and $lt:

    db.CollectionName.find({"whenCreated": {
        '$gte': ISODate("2018-03-06T13:10:40.294Z"),
        '$lt': ISODate("2018-05-06T13:10:40.294Z")
    }});
    
    0 讨论(0)
提交回复
热议问题