在MongoDB中的另一个数组的日期字符串阵列中应用日期过滤器

发布于 2025-02-09 06:12:59 字数 2724 浏览 0 评论 0原文

我有一个包含一个称为“字段”的对象数组的集合,我还有另一个称为“值”的字符串数组,该数组在“字段”数组中。

现在,我必须使用日期过滤,但是该值保持内部值数组。

请帮助我形成查询。

像下面的收集对象一样,

    {
       "_id" : ObjectId("62aeb8301ed12a14a8873df1"),
       "Fields" : [ 
        {
            "FieldId" : "e8efd0b0-9d10-4584-bb11-5b24f189c03b",
            "Value" : [ 
                "test_123"
            ]
        }, 
        {
            "FieldId" : "fa6745c2-b259-4a3b-8c6f-19eb78fbbbf5",
            "Value" : [ 
                "123"
            ]
        }, 
        {
            "FieldId" : "2a1be5d0-8fb6-4b06-a253-55337bfe4bcd",
            "Value" : []
        }, 
        {
            "FieldId" : "eed12747-0923-4290-b09c-5a05107f5609",
            "Value" : [ 
                "234234234"
            ]
        },        
        {
            "FieldId" : "fe41d8fb-fa18-4fe5-b047-854403aa4d84",
            "Value" : [ 
                "Irrelevan"
            ]
        }, 
        {
            "FieldId" : "93e46476-bf2e-44eb-ac73-134403220e9e",
            "Value" : [ 
                "test"
            ]
        }, 
        {
            "FieldId" : "db434aca-8df3-4caf-bdd7-3ec23252c2c8",
            "Value" : [ 
                "2019-06-16T18:30:00.000Z"
            ]
        }, 
        {
            "FieldId" : "00df903f-5d59-41c1-a3df-60eeafb77d10",
            "Value" : [ 
                "tewt"
            ]
        }, 
        {
            "FieldId" : "e97d0386-cd42-6277-1207-e674c3268cec",
            "Value" : [ 
                "1"
            ]
        },
        {
            "FieldId" : "35e55d27-7d2c-467d-8a88-09ad6c9f5631",
            "Value" : [ 
                "10"
            ]
        }
      ]
     }

我的查询如下,

    db.getCollection('Table1').find({
    $and:[
        {FieldsDatas:{$elemMatch:{FieldId:'e8efd0b0-9d10-4584-bb11-5b24f189c03b',Value:{$regex:'^mani',$options:'i'}}}},
        {FieldsDatas:{$elemMatch:{FieldId:'e97d0386-cd42-6277-1207-e674c3268cec',Value:{$in:['1','2','1,2']}}}},
        {FieldsDatas:{$elemMatch:{FieldId:'35e55d27-7d2c-467d-8a88-09ad6c9f5631',Value:'10'}}},
        {FieldsDatas:{$elemMatch:{FieldId:'db434aca-8df3-4caf-bdd7-3ec23252c2c8','$first':
            {Value:$gt:[{$toDate:'$this'},Date('2022-06-16')]}
        }}}
    ]})

您可以看到日期可在“ 2019-06-16T18:30:00.000Z”中可用。我们必须仅对该字段应用过滤器。

注意:值数组只有一个值

$ todate过滤器不起作用,并且日期过滤器也无法正常工作。请找到以下链接,其中包含样品,

htttps://mongoplay.net.net/p/sroayhwvig2

请建议我您的答案解决这个问题。

提前致谢, 玛尼

I am having a collection which holding array of objects which is called as "Fields" and I am having another string array called as "Value" which is inside "Fields" array.

Now I have to filter using date but the value is holding inside Value array.

Please help me to form the query.

The collection object like below,

    {
       "_id" : ObjectId("62aeb8301ed12a14a8873df1"),
       "Fields" : [ 
        {
            "FieldId" : "e8efd0b0-9d10-4584-bb11-5b24f189c03b",
            "Value" : [ 
                "test_123"
            ]
        }, 
        {
            "FieldId" : "fa6745c2-b259-4a3b-8c6f-19eb78fbbbf5",
            "Value" : [ 
                "123"
            ]
        }, 
        {
            "FieldId" : "2a1be5d0-8fb6-4b06-a253-55337bfe4bcd",
            "Value" : []
        }, 
        {
            "FieldId" : "eed12747-0923-4290-b09c-5a05107f5609",
            "Value" : [ 
                "234234234"
            ]
        },        
        {
            "FieldId" : "fe41d8fb-fa18-4fe5-b047-854403aa4d84",
            "Value" : [ 
                "Irrelevan"
            ]
        }, 
        {
            "FieldId" : "93e46476-bf2e-44eb-ac73-134403220e9e",
            "Value" : [ 
                "test"
            ]
        }, 
        {
            "FieldId" : "db434aca-8df3-4caf-bdd7-3ec23252c2c8",
            "Value" : [ 
                "2019-06-16T18:30:00.000Z"
            ]
        }, 
        {
            "FieldId" : "00df903f-5d59-41c1-a3df-60eeafb77d10",
            "Value" : [ 
                "tewt"
            ]
        }, 
        {
            "FieldId" : "e97d0386-cd42-6277-1207-e674c3268cec",
            "Value" : [ 
                "1"
            ]
        },
        {
            "FieldId" : "35e55d27-7d2c-467d-8a88-09ad6c9f5631",
            "Value" : [ 
                "10"
            ]
        }
      ]
     }

I am having query like,

    db.getCollection('Table1').find({
    $and:[
        {FieldsDatas:{$elemMatch:{FieldId:'e8efd0b0-9d10-4584-bb11-5b24f189c03b',Value:{$regex:'^mani',$options:'i'}}}},
        {FieldsDatas:{$elemMatch:{FieldId:'e97d0386-cd42-6277-1207-e674c3268cec',Value:{$in:['1','2','1,2']}}}},
        {FieldsDatas:{$elemMatch:{FieldId:'35e55d27-7d2c-467d-8a88-09ad6c9f5631',Value:'10'}}},
        {FieldsDatas:{$elemMatch:{FieldId:'db434aca-8df3-4caf-bdd7-3ec23252c2c8','$first':
            {Value:$gt:[{$toDate:'$this'},Date('2022-06-16')]}
        }}}
    ]})

you can see date is available inside Value array "2019-06-16T18:30:00.000Z". we have to apply filter for that field only.

Note : Value array have only one value

$toDate filter is not working and date filter also not working. Please find the below link where it has sample,

https://mongoplayground.net/p/sroaYHWViG2

Please suggest me your answers to resolve this.

Thanks in advance,
Mani

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2025-02-16 06:12:59

您可以使用$ MAP$ redy从双嵌套数组中提取日期。使用$ convert使其成为正确的日期对象,并使用onerror选项使其他非日期值为null。最后,再次使用$降低再次提取唯一的日期对象,并在其上执行$ match

db.collection.aggregate([
  {
    "$addFields": {
      "dateForFilter": {
        "$map": {
          "input": "$Fields",
          "as": "f",
          "in": {
            "$reduce": {
              "input": "$f.Value",
              "initialValue": null,
              "in": {
                "$convert": {
                  "input": "$this",
                  "to": "date",
                  "onError": null,
                  "onNull": null
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "dateForFilter": {
        "$reduce": {
          "input": "$dateForFilter",
          "initialValue": null,
          "in": {
            "$cond": {
              "if": {
                $ne: [
                  "$this",
                  null
                ]
              },
              "then": "$this",
              "else": "$value"
            }
          }
        }
      }
    }
  },
  {
    $match: {
      dateForFilter: {
        $lt: ISODate("2022-06-16")
      }
    }
  }
])

这是 mongo playground 供您参考。

You can use $map and $reduce to extract the date out from doubly nested array. Use $convert to make it a proper date object and use the onError option to make other non-date value to be null. Finally use $reduce again to extract the only date object again and perform $match on it.

db.collection.aggregate([
  {
    "$addFields": {
      "dateForFilter": {
        "$map": {
          "input": "$Fields",
          "as": "f",
          "in": {
            "$reduce": {
              "input": "$f.Value",
              "initialValue": null,
              "in": {
                "$convert": {
                  "input": "$this",
                  "to": "date",
                  "onError": null,
                  "onNull": null
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "dateForFilter": {
        "$reduce": {
          "input": "$dateForFilter",
          "initialValue": null,
          "in": {
            "$cond": {
              "if": {
                $ne: [
                  "$this",
                  null
                ]
              },
              "then": "$this",
              "else": "$value"
            }
          }
        }
      }
    }
  },
  {
    $match: {
      dateForFilter: {
        $lt: ISODate("2022-06-16")
      }
    }
  }
])

Here is the Mongo Playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文