【问题标题】:converting the datestring in a nested array of objects (MongoDB)转换嵌套对象数组中的日期字符串(MongoDB)
【发布时间】:2021-10-14 08:27:48
【问题描述】:

我想要实现的是根据提供的日期在当月查找特定文档。日期存储为字符串,以便我比较需要先转换日期的日期。但是,在嵌套对象数组中转换日期字符串时遇到了麻烦。

我的收藏:

{
    sections: [{
            fields: [{
                    name: 'Date',
                    value: '2020-11-30T15:59:59.999Z' // this is string 
                },
                {
                    name: 'Title',
                    value: 'My book' 
                },
                {
                    name: 'Author',
                    value: 'Henry'
                }
            ]
        ]
    }
}

我尝试过的:

1)

const existingReport = await Report.find({
      $expr: {
        $gte: [
          {
            $dateFromString: {
              dateString: "$sections.field[0].value",
            },
          },
          moment(payload.forPeriod).startOf("month").toDate(),
        ],
        $lt: [
          {
            $dateFromString: {
              dateString: "$sections.field[0].value",
            },
          },
          moment(payload.forPeriod).endOf("month").toDate(),
        ],
      },
    });
const existingReport1 = await Report.aggregate([
      {
        $addFields: {
          formattedData: {
            $cond: {
              if: {
                $eq: ["$sections.field.value", "Date"],
              },
              then: {
                $dateFromString: {
                  dateString: "$sections.field.value",
                },
              },
              else: "$sections.field.value",
            },
          },
        },
      },
    ]);

【问题讨论】:

    标签: javascript node.js mongodb mongodb-query


    【解决方案1】:

    您可以在 2 $reduce 的帮助下简单地执行 $toDate 来迭代 sectionsfields 数组。

    db.collection.aggregate([
      {
        "$match": {
          $expr: {
            $eq: [
              true,
              {
                "$reduce": {
                  "input": "$sections",
                  "initialValue": false,
                  "in": {
                    "$reduce": {
                      "input": "$$this.fields",
                      "initialValue": false,
                      "in": {
                        $or: [
                          "$$value",
                          {
                            $and: [
                              {
                                $gte: [
                                  {
                                    "$toDate": "$$this.value"
                                  },
                                  new Date("2020-11-01")
                                ]
                              },
                              {
                                $lte: [
                                  {
                                    "$toDate": "$$this.value"
                                  },
                                  new Date("2020-11-30")
                                ]
                              }
                            ]
                          }
                        ]
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      }
    ])
    

    这是Mongo playground 供您参考。

    【讨论】:

    • 我仍然无法过滤掉正确的文档。我编辑了你提供的playground,可以帮我看看,谢谢。 mongoplayground.net/p/gwFVoi_miW_
    • @Thomas 更新了答案以使用 $reduce 以更好地满足您的需求。
    • 感谢您的帮助,但是一旦我更改日期,就会发生错误
    • 错误信息是什么?您最近的试用期是什么?
    • 在尝试转换之前尝试this类似的代码,并额外检查名称字段
    猜你喜欢
    • 2020-05-04
    • 1970-01-01
    • 2020-03-23
    • 2018-06-26
    • 1970-01-01
    • 2022-01-14
    • 2019-12-18
    • 2023-03-12
    • 2020-05-21
    相关资源
    最近更新 更多