使用dateFromString聚合匹配Mongodb

使用dateFromString聚合匹配Mongodb,mongodb,Mongodb,亲爱的尝试在mongo中按日期筛选,但我没有成功。我传递JSON的查询和结构 db.getCollection('articles').aggregate([ { "$match": { "$expr": { "$and": [ { "$gte": [ { "$dateFromString": { "creationDate": "10-08-2018", "format": "%m-%d-%Y" }} ] }, {

亲爱的尝试在mongo中按日期筛选,但我没有成功。我传递JSON的查询和结构

db.getCollection('articles').aggregate([
{ "$match": {
 "$expr": {
  "$and": [
    {
      "$gte": [
        { "$dateFromString": { "creationDate": "10-08-2018", "format": "%m-%d-%Y" }}
      ]
    },
    {
      "$lte": [
        { "$dateFromString": { "creationDate": "10-08-2018", "format": "%m-%d-%Y" }}
      ]
    }
  ]
}
}}
])
我的JSON是

{
"_id" : ObjectId("5bbb6b1de75b933850a608fc"),
"title" : "05",
"body" : "asgfasgasfa",
"creationDate" : ISODate("2018-10-08T14:35:07.000Z"),
"operationType" : "C",
"__v" : 0
}

MongoDB:v3.6.3

如果您在“2018年10月8日”的某个日期字段中查找匹配项,则可以与
$eq
结合使用:

db.getCollection('articles').aggregate([
  {
    "$match": {
      "$expr": {
        $eq: [
          "10-08-2018",
          {
            "$dateToString": {
              "date": "$creationDate",
              "format": "%m-%d-%Y"
            }
          }
        ]
      }
    }
  }
])
看到它工作了吗

如果要查找与日期范围匹配的一组记录:

db.getCollection('articles').aggregate([
  {
    "$match": {
      "$expr": {
        "$and": [
          {
            "$gte": [
              "$creationDate",
              {
                "$dateFromString": {
                  "dateString": "10-08-2018",
                  "format": "%m-%d-%Y"
                }
              }
            ]
          },
          {
            "$lte": [
              "$creationDate",
              {
                "$dateFromString": {
                  "dateString": "11-08-2018",
                  "format": "%m-%d-%Y"
                }
              }
            ]
          }
        ]
      }
    }
  }
])
看到它工作了吗


第二个例子中的一个注意事项是,它将日期作为ISO日期,因此它不会覆盖一天的结束日期11-08-2018,而是2018-11-08T00:00:00.000Z,正如
matthPen
注释所指出的那样。

即使@Akrion answer返回样本,它也不会正确过滤日期。从全局角度看,最好尽可能频繁地处理日期,而不是转换为字符串。一个简单的例子:使用您的格式(“%m-%d-%Y”)、日期(“10-08-2018”)日期(“12-01-2017”),但在字符串比较方面,“10-08-2018”将$lte部分更改为
{“$lte”:[{“$dateFromString”:{“creationDate”:“11-08-2018”,“格式”:“%m-%d-%Y”}}
您是在寻找精确的值还是值的范围?由于您使用的是一个范围(
从:gte
-
到:lte
),因此不清楚其意图。否!您的两个答案都不起作用:尝试使用扩展样本集,包括您不想要的值(2018年10月8日(2018年10月8日,格式%m-%d-%Y)以外的任何值)。请参见抱歉,但不是。尝试您的示例,两个结果中的一个不是预期的结果。为什么?我正在寻找一个范围,而不是确切的日期值。两者都在
10-08-2018
11-08-2018
的范围内。我看到了这一点,但不确定这个问题要求的日期范围。再看看您的示例:由于isoDate的时间部分,第二个文档没有返回。未采用ISODate(“2018-11-08T14:35:07.000Z”),而采用ISODate(“2018-11-08T00:00:00.000Z”)。使用这种方法,时区会带来真正的麻烦。
db.collection.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          {
            $eq: [
              {
                $year: new Date("2018-10-08")
              },
              {
                $year: "$creationDate"
              }
            ]
          },
          {
            $eq: [
              {
                $month: new Date("2018-10-08")
              },
              {
                $month: "$creationDate"
              }
            ]
          },
          {
            $eq: [
              {
                $dayOfMonth: new Date("2018-10-08")
              },
              {
                $dayOfMonth: "$creationDate"
              }
            ]
          }
        ]
      }
    }
  }
])
db.collection.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          {
// Start date definition, included ($gte)
            $or: [
              {
                $and: [
                  {
                    $eq: [
                      {
                        $year: new Date("2018-10-08")
                      },
                      {
                        $year: "$creationDate"
                      }
                    ]
                  },
                  {
                    $eq: [
                      {
                        $month: new Date("2018-10-08")
                      },
                      {
                        $month: "$creationDate"
                      }
                    ]
                  },
                  {
                    $gte: [
                      {
                        $dayOfMonth: "$creationDate"
                      },
                      {
                        $dayOfMonth: new Date("2018-10-08")
                      }
                    ]
                  }
                ]
              },
              {
                $and: [
                  {
                    $eq: [
                      {
                        $year: "$creationDate"
                      },
                      {
                        $year: new Date("2018-10-08")
                      }
                    ]
                  },
                  {
                    $gte: [
                      {
                        $month: "$creationDate"
                      },
                      {
                        $month: new Date("2018-10-08")
                      }
                    ]
                  },

                ]
              },
              {
                $and: [
                  {
                    $gte: [
                      {
                        $year: "$creationDate"
                      },
                      {
                        $year: new Date("2018-10-08")
                      }
                    ]
                  },

                ]
              }
            ],

          },
     //end date definition, excluded ($lt)
          {
            $or: [
              {
                $and: [
                  {
                    $eq: [
                      {
                        $year: new Date("2018-11-08")
                      },
                      {
                        $year: "$creationDate"
                      }
                    ]
                  },
                  {
                    $eq: [
                      {
                        $month: new Date("2018-11-08")
                      },
                      {
                        $month: "$creationDate"
                      }
                    ]
                  },
                  {
                    $lt: [
                      {
                        $dayOfMonth: "$creationDate"
                      },
                      {
                        $dayOfMonth: new Date("2018-11-08")
                      }
                    ]
                  }
                ]
              },
              {
                $and: [
                  {
                    $eq: [
                      {
                        $year: "$creationDate"
                      },
                      {
                        $year: new Date("2018-10-08")
                      }
                    ]
                  },
                  {
                    $lt: [
                      {
                        $month: "$creationDate"
                      },
                      {
                        $month: new Date("2018-11-08")
                      }
                    ]
                  },

                ]
              },
              {
                $and: [
                  {
                    $lt: [
                      {
                        $year: "$creationDate"
                      },
                      {
                        $year: new Date("2018-11-08")
                      }
                    ]
                  },

                ]
              }
            ],

          }
        ]
      }
    }
  }
])