Couchbase:如何根据子值从列表中删除项目?

问题描述 投票:2回答:2

Couchbase 2.5+

这是沙发基地的新手。我希望根据子项的值从列表中删除其中一个子项的项。例如,如果我想删除列表scores中孩子score < 0.2所有记录

此商品有一把钥匙"dim::100020891::2020-10-04"

我想过运行它,但它没有用

UPDATE Q1036628 USE KEYS "dim::100020891::2020-10-04" SET scores=ARRAY_REMOVE(scores, {
      "score"< "0.2"
    })

Couchbase项目:

   [
          {
            "Q1036628": {
              "record_update_dt": "2020-10-04",
              "scores": [
                {
                  "name": "A",
                  "score": 0.01
                },
                {
                  "name": "B",
                  "score": 0.5
                },
                {
                  "name": "C",
                  "score": 0.26
                },
                {
                  "name": "D",
                  "score": 0.17
                }
              ],
              "id": "100020891"
            }
          }
        ]
nosql couchbase
2个回答
1
投票

您应该使用ARRAY子句。

UPDATE Q1036628 USE KEYS "dim::100020891::2020-10-04" 
SET scores = ARRAY s FOR s IN scores WHEN s.score >= 0.2 END

https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/collectionops.html#array

您可能需要稍微使用语法;我没有方便的实例。


1
投票

只需在Johan Larson解决方案中再添加一个。

WHERE子句控制要变异的文档。 SET子句控制要更新的内容。您在SET子句中有条件。如果得分数组中没有任何对象,得分<0.2仍然是变异的(即它更新相同的值)。为了避免这种情况,你需要有WHERE子句。

UPDATE Q1036628 AS d USE KEYS "dim::100020891::2020-10-04" 
SET d.scores = ARRAY s FOR s IN d.scores WHEN s.score >= 0.2 END
WHERE ANY s IN d.scores SATISFIES s.score < 0.2 END;
© www.soinside.com 2019 - 2024. All rights reserved.