$dateFromString将使用mongodb将此月名转换为数字

$dateFromString将使用mongodb将此月名转换为数字,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有一个收藏,预计日期为字符串格式“2019年9月19日”。如何基于预期的日期匹配编写聚合查询。 下面是我的收藏 /* 1 */ { "_id" : ObjectId("5ceb7e71636566fe7718733e"), "project_name" : "p1", "expected" : "19-Sep-2019", "actual" : "19-Sep-2019" } /* 2 */ { "_id" : ObjectId("5ceb7e7163

我有一个收藏,预计日期为字符串格式“2019年9月19日”。如何基于预期的日期匹配编写聚合查询。 下面是我的收藏

/* 1 */
{
    "_id" : ObjectId("5ceb7e71636566fe7718733e"),
    "project_name" : "p1",
    "expected" : "19-Sep-2019",
    "actual" : "19-Sep-2019"
}

/* 2 */
{
    "_id" : ObjectId("5ceb7e71636566fe77187340"),
    "project_name" : "p2",
    "expected" : "20-Sep-2019",
    "actual" : "20-Sep-2019"
}

/* 3 */
{
    "_id" : ObjectId("5ceb7e71636566fe77187342"),
    "project_name" : "p3",
    "expected" : "19-Jan-2020",
    "actual" : "19-Jan-2020"
}

/* 4 */
{
    "_id" : ObjectId("5ceb7e71636566fe77187344"),
    "project_name" : "p4",
    "expected" : "20-Jan-2020",
    "actual" : "20-Jan-2020"
}
我需要写一个如下的查询。但是它获取所有的值。我如何使用$dateFromString实现此目的

db.getCollection('test2').aggregate([
  {"$match":{"expected":{"$gte":"01-Sep-2019","$lte":"30-Sep-2019"}}}
 ])
预期结果是

{
    "_id" : ObjectId("5ceb7e71636566fe7718733e"),
    "project_name" : "p1",
    "expected" : "19-Sep-2019",
    "actual" : "19-Sep-2019"
}
{
    "_id" : ObjectId("5ceb7e71636566fe77187340"),
    "project_name" : "p2",
    "expected" : "20-Sep-2019",
    "actual" : "20-Sep-2019"
}
$dateFromString
不支持
MMM
缩写模式 解决方法1:我们使用月份名称(“一月”、“二月”、“十二月”)创建数组,并使用与我们创建的
ISODate
的月份+运算符等效的数组索引

db.collection.aggregate([
  {
    $addFields: {
      months: [
        "",
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
      ]
    }
  },
  {
    $addFields: {
      date: {
        $dateFromParts: {
          year: {
            $toInt: {
              $substr: [
                "$expected",
                7,
                4
              ]
            }
          },
          month: {
            $indexOfArray: [
              "$months",
              {
                $substr: [
                  "$expected",
                  3,
                  3
                ]
              }
            ]
          },
          day: {
            $toInt: {
              $substr: [
                "$expected",
                0,
                2
              ]
            }
          }
        }
      }
    }
  },
  {
    $match: {
      date: {
        $gte: ISODate("2019-09-01"),
        $lte: ISODate("2019-09-30")
      }
    }
  },
  {
    $unset: [
      "months",
      "date"
    ]
  }
])
{
   $switch: {
      branches: [
         { case: { $eq: [ "Jan", {$substr:["$expected", 3, 3]} ] }, then: 1 },
         { case: { $eq: [ "Feb", {$substr:["$expected", 3, 3]} ] }, then: 2 },
         { case: { $eq: [ "Sep", {$substr:["$expected", 3, 3]} ] }, then: 9 },
         { case: { $eq: [ "Dec", {$substr:["$expected", 3, 3]} ] }, then: 12 }
      ],
      default: null
   }
}

解决方案2:您需要使用运算符转换月份:
Jan
-1、
Feb
-2<代码>Sep-9,我们使用操作符创建
ISODate

db.collection.aggregate([
  {
    $addFields: {
      months: [
        "",
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
      ]
    }
  },
  {
    $addFields: {
      date: {
        $dateFromParts: {
          year: {
            $toInt: {
              $substr: [
                "$expected",
                7,
                4
              ]
            }
          },
          month: {
            $indexOfArray: [
              "$months",
              {
                $substr: [
                  "$expected",
                  3,
                  3
                ]
              }
            ]
          },
          day: {
            $toInt: {
              $substr: [
                "$expected",
                0,
                2
              ]
            }
          }
        }
      }
    }
  },
  {
    $match: {
      date: {
        $gte: ISODate("2019-09-01"),
        $lte: ISODate("2019-09-30")
      }
    }
  },
  {
    $unset: [
      "months",
      "date"
    ]
  }
])
{
   $switch: {
      branches: [
         { case: { $eq: [ "Jan", {$substr:["$expected", 3, 3]} ] }, then: 1 },
         { case: { $eq: [ "Feb", {$substr:["$expected", 3, 3]} ] }, then: 2 },
         { case: { $eq: [ "Sep", {$substr:["$expected", 3, 3]} ] }, then: 9 },
         { case: { $eq: [ "Dec", {$substr:["$expected", 3, 3]} ] }, then: 12 }
      ],
      default: null
   }
}

$dateFromString
不支持
MMM
缩写模式 解决方法1:我们使用月份名称(“一月”、“二月”、“十二月”)创建数组,并使用与我们创建的
ISODate
的月份+运算符等效的数组索引

db.collection.aggregate([
  {
    $addFields: {
      months: [
        "",
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
      ]
    }
  },
  {
    $addFields: {
      date: {
        $dateFromParts: {
          year: {
            $toInt: {
              $substr: [
                "$expected",
                7,
                4
              ]
            }
          },
          month: {
            $indexOfArray: [
              "$months",
              {
                $substr: [
                  "$expected",
                  3,
                  3
                ]
              }
            ]
          },
          day: {
            $toInt: {
              $substr: [
                "$expected",
                0,
                2
              ]
            }
          }
        }
      }
    }
  },
  {
    $match: {
      date: {
        $gte: ISODate("2019-09-01"),
        $lte: ISODate("2019-09-30")
      }
    }
  },
  {
    $unset: [
      "months",
      "date"
    ]
  }
])
{
   $switch: {
      branches: [
         { case: { $eq: [ "Jan", {$substr:["$expected", 3, 3]} ] }, then: 1 },
         { case: { $eq: [ "Feb", {$substr:["$expected", 3, 3]} ] }, then: 2 },
         { case: { $eq: [ "Sep", {$substr:["$expected", 3, 3]} ] }, then: 9 },
         { case: { $eq: [ "Dec", {$substr:["$expected", 3, 3]} ] }, then: 12 }
      ],
      default: null
   }
}

解决方案2:您需要使用运算符转换月份:
Jan
-1、
Feb
-2<代码>Sep-9,我们使用操作符创建
ISODate

db.collection.aggregate([
  {
    $addFields: {
      months: [
        "",
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
      ]
    }
  },
  {
    $addFields: {
      date: {
        $dateFromParts: {
          year: {
            $toInt: {
              $substr: [
                "$expected",
                7,
                4
              ]
            }
          },
          month: {
            $indexOfArray: [
              "$months",
              {
                $substr: [
                  "$expected",
                  3,
                  3
                ]
              }
            ]
          },
          day: {
            $toInt: {
              $substr: [
                "$expected",
                0,
                2
              ]
            }
          }
        }
      }
    }
  },
  {
    $match: {
      date: {
        $gte: ISODate("2019-09-01"),
        $lte: ISODate("2019-09-30")
      }
    }
  },
  {
    $unset: [
      "months",
      "date"
    ]
  }
])
{
   $switch: {
      branches: [
         { case: { $eq: [ "Jan", {$substr:["$expected", 3, 3]} ] }, then: 1 },
         { case: { $eq: [ "Feb", {$substr:["$expected", 3, 3]} ] }, then: 2 },
         { case: { $eq: [ "Sep", {$substr:["$expected", 3, 3]} ] }, then: 9 },
         { case: { $eq: [ "Dec", {$substr:["$expected", 3, 3]} ] }, then: 12 }
      ],
      default: null
   }
}


以下聚合可用于匹配
预期的
日期字符串

考虑输入文档:

查询:


结果文档:
{“预期”:“2020-01-20”}

以下聚合可用于匹配
预期的日期字符串

考虑输入文档:

查询:

结果文件:
{“预期”:“2020-01-20”}