Mongodb (v2.4.0) $match aggregate not working with date range

前端 未结 2 865
面向向阳花
面向向阳花 2021-01-14 08:46

I am using mongodb java driver thru maven repository (as below in pom.xml) to query transactions between date range with aggregate framwork. The java driver generates follo

相关标签:
2条回答
  • 2021-01-14 09:06

    You have to format the date before passing onto $match aggregate.

    Order.aggregate([
            {
              $match: {
                createdAt: {
                  $gte: new Date(req.body.startDate),
                  $lt: new Date(req.body.endDate)
                }
              }
            },
            {
              $lookup: {
                from: 'acbinstallerpayments',
                localField: "_id",
                foreignField: 'customerObjectID',
                as: 'installerPaymentDetails'
              }
            }
          ]);
    
    0 讨论(0)
  • 2021-01-14 09:26

    I got it solved by removing the "" & $ prefix on the $date field of in $match. For you remove the same for $date, $gt & $lte

    So that it should look like

    db.transactions.aggregate(
    { "$match" : 
             { 
              'created_at': { 
                             $gt: "2001-04-12T12:00:00.000Z", 
                             $lt: "2020-04-13T12:00:00.000Z"
                            }
             }
    });
    
    0 讨论(0)
提交回复
热议问题