如何在mongodb嵌套数组中按日期过滤?

问题描述 投票:0回答:1

我需要报告。我的数据是嵌套的。我想按日期范围过滤。日期字段字符串。我的Mongo版本3.4.2

我想在cosDate中过滤2019年的内容。也就是说,我的目标是列出1.1.2019-30.12.2019分钟。以下是我相关收藏的图片

enter image description here我的示例mongo集合https://jsoneditoronline.org/?id=bcac623bb8cd401dbdb62f48bd1c3c68

mongodb robo3t
1个回答
0
投票

aggregate函数与filter一起使用,我们可以根据日期过滤数据。

db.demo.aggregate(
      { "$addFields": {
        "costs": 
          {
              "$filter": {
                      "input": "$costs",
                      "as": "sn",
                      "cond": {
                        "$and": [
                          { "$gte": [ "$$sn.costDate", "01/01/2019" ] },
                           { "$lte": [ "$$sn.costDate", "12/01/2019" ] },
                     ]
              },


          }
       } 

    }}
)
© www.soinside.com 2019 - 2024. All rights reserved.