【问题标题】:Need to filter mongo document containing array of object basis on bussines logic需要根据业务逻辑过滤包含对象数组的mongo文档
【发布时间】:2022-09-23 03:45:39
【问题描述】:
  {
    \"id\": 1,
    \"holdDetails\": [
      {
        \"holdDescription\": \"COVERT_LOCK\",
        \"holdStatus\": \"CREATED\"
      },
      {
        \"holdDescription\": \"ALPH_LOCK\",
        \"holdStatus\": \"RESOLVED\"
      }
    ]
  },
  {
    \"id\": 2,
    \"holdDetails\": [
      {
        \"holdDescription\": \"ALPHA_LOCK\",
        \"holdStatus\": \"RESOLVED\"
      },
      {
        \"holdDescription\": \"BETA_LOCK\",
        \"holdStatus\": \"RESOLVED\"
      }
    ]
  },
  {
    \"id\": 3,
    \"holdDetails\": [
      {
        \"holdDescription\": \"ALPHA_LOCK\",
        \"holdStatus\": \"CREATED\"
      },
      {
        \"holdDescription\": \"BETA_LOCK\",
        \"holdStatus\": \"CREATED\"
      }
    ]
  }
]

现在我想根据获取所有这些对象的条件过滤此文档 其中所有的holdDetails都在RESOLVED中holdStatus(即COVERT_HOLD也在RESOLVED中 holdStatus) 或所有这些都处于 RESOLVED 除了 COVERT_LOCK 处于 CREATED 状态之外的保持状态

所以发布这个条件结果应该是

  {
    \"id\": 1,
    \"holdDetails\": [
      {
        \"holdDescription\": \"COVERT_LOCK\",
        \"holdStatus\": \"CREATED\"
      },
      {
        \"holdDescription\": \"ALPH_LOCK\",
        \"holdStatus\": \"RESOLVED\"
      }
    ]
  },
  {
    \"id\": 2,
    \"holdDetails\": [
      {
        \"holdDescription\": \"ALPHA_LOCK\",
        \"holdStatus\": \"RESOLVED\"
      },
      {
        \"holdDescription\": \"BETA_LOCK\",
        \"holdStatus\": \"RESOLVED\"
      }
    ]
  }
]

在 java 中创建的 Query.class 对象应该是什么来产生这样的结果?

  • 所以我想要所有类型的holdDescription都在RESOLVED holdStatus中的所有对象,不包括COVERT_LOCK的holdStatus。

标签: java mongodb spring-boot mongodb-query spring-data-mongodb


【解决方案1】:

您可以为此使用 $filter$or

db.collection.aggregate([
  {$match: {
      $expr: {
        $eq: [
          {$size: "$holdDetails"},
          {$size: {
              $filter: {
                input: "$holdDetails",
                cond: {
                  $or: [
                    {$eq: ["$$this.holdStatus", "RESOLVED"]},
                    {$eq: ["$$this.holdDescription", "COVERT_LOCK"]}
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
])

playground example 上查看它是如何工作的

【讨论】:

  • 嘿@nimrod-serok 你能帮我在标准查询Java中制定这个吗?
  • 我不熟悉这个
  • 查询问题::: 完整响应是: { "ok" : 0.0, "errmsg" : "PlanExecutor error during aggregation :: 由 :: The argument to $size must be an array, but is of type: missing ", "code" : 17124.0, "codeName" : "Location17124", "$clusterTime" : { "clusterTime" : Timestamp(1663235234, 1), "signature" : { "hash" : BinData(0, "YlOwObuxEf3L2dG41tkNDa1e8Rc=" ), "keyId" : NumberLong(7083587006063706114) } }, "operationTime" : 时间戳(1663235234, 1) }
  • 请提供带有案例的mongodb游乐场链接
  • 这意味着holdDetails 不是一个数组(至少在一个文档中),但在您提供的示例数据中它是一个数组。
【解决方案2】:

你可以试试这个:

Query query = Query.query(where("holdDetails.holdStatus").is("RESOLVED"));

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 2022-11-29
    • 2015-12-05
    • 1970-01-01
    • 2022-08-15
    相关资源
    最近更新 更多