【问题标题】:spring mongodb criteria API: check two values on the same nested elementspring mongodb标准API:检查同一个嵌套元素上的两个值
【发布时间】:2019-02-13 12:06:36
【问题描述】:

我有以下疑问:

Criteria crit = Criteria.where("nestedObj.date").lt(LocalDate.now())
        .and("nestedObj.active").is(true)
        .and("someId").is(null)
        .and("somethingElse").exists(false);

如何确保nestedObj.activenestedObj.date 在同一个nestedObj 上被选中?

我只希望在文档具有活动的nestedObj 并且日期早于今天的情况下匹配。

例子:

如果文档上的 nestedObj 数组像这样丢失,则查询应该匹配:

[
    {
    "nestedObj": {
        "active": "true",
        "date": "2010-29-10"
    },
    {
    "nestedObj": {
        "active": "false",
        "date": "2010-29-10"
    },
    {
    "nestedObj": {
        "active": "true",
        "date": "2022-29-10"
    }
]

但如果它看起来像这样,它不应该:

[
    {
    "nestedObj": {
        "active": "false",
        "date": "2010-29-10"
    },
    {
    "nestedObj": {
        "active": "true",
        "date": "2022-29-10"
    }
]

【问题讨论】:

    标签: spring mongodb criteria-api


    【解决方案1】:

    检查https://docs.mongodb.com/manual/reference/operator/query/elemMatch/中的元素匹配

    例如

    where("nestedObj.date").elemMatch( where("attribute1").is("value1").and("attribute2").regex("(?i).*$something.*")

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 1970-01-01
      • 2020-02-22
      • 2022-01-24
      • 2014-03-09
      • 2018-12-13
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多