Couchbase按模式删除属性

huangapple go评论57阅读模式
英文:

Couchbase remove attributes by pattern

问题

由于应用程序中的错误,不希望的以“}__modified”结尾的属性被插入到Bucket中。我想从Bucket中删除所有这些属性("UNSET")。以下是实现此目标的正确N1QL语法:

UPDATE mainstore
SET
  mainstore = OBJECT_REMOVE(mainstore,
    (SELECT RAW attr
     FROM OBJECT_NAMES(mainstore)
     WHERE REGEXP_LIKE(attr, ".*}__modified$"))
  )
WHERE
  TYPE(mainstore) = "object";

请确保在运行此语句之前备份您的数据,以防意外情况发生。

英文:

I have a Couchbase bucket with simple document objects.
Due to a bug in the application unwanted attributes with names that end with '}__modified' were inserted into the bucket. I want to remove all these attributes ("UNSET") from the bucket. What would be the right N1QL syntax to accomplish that?

SELECT * FROM mainstore LIMIT 3

  {
    "mainstore": {
      "class_id": "9853ede22ad54d4da86c6d9abc91110a",
      "class_name": "BLE_34V2",
      "company": "76092d9bb1b148d18c3484f447a554b4",
      "connection_type": "gateway",
	  "conns}__modified":45,
	  "conns5}__modified":45,
	  "clsd5}__modified":45
	},
    "mainstore": {
      "class_id": "6443ede22ad54d4da86c6d9abc91110a",
      "class_name": "BLE_34V2",
      "company": "76092d9bb1b148d18c3484f447a554b4",
      "connection_type": "client",
	  "conns}__modified":45,
	  "consf5}__modified":46,
	  "clsd5}__modified":46
	},
    "mainstore": {
      "class_id": "1343ede22ad54d4da86c6d9abc91110a",
      "class_name": "BLE_34V2",
      "company": "76092d9bb1b148d18c3484f447a554b4",
      "connection_type": "client",
	  "dfffs}__modified":45,
	  "dffs5}__modified":47,
	  "fdss5}__modified":46
	}
  }

答案1

得分: 1

以下是翻译好的代码部分:

假设这些是顶级属性

创建索引 ix1 ON mainstore (
           CONTAINS(encode_json(OBJECT_NAMES(self)), ""}"")
           ) 
    WHERE CONTAINS(encode_json(OBJECT_NAMES(self)), ""}"") = true;

如果您想从字段名称中移除 "}"

UPDATE mainstore AS m
SET m = OBJECT REPLACE(n,"}"",""):v FOR n:v IN m END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), ""}"") = true;

如果您想从文档中移除包含 "}" 的所有字段

UPDATE mainstore AS m
SET m = OBJECT n:v FOR n:v IN m  WHEN POSITION(n,"}&quot;") < 0 END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), "&quot;}&quot;") = true;

如果您有大量文档,可能会变慢。如果使用 EE,请使用事件处理来更新它们。您可以在这里找到示例:https://docs.couchbase.com/server/current/eventing/eventing-examples.html

请注意,由于文本中包含特殊字符,我已将它们保留在翻译中。

英文:

ASSUME those are top level attributes

CREATE INDEX ix1 ON mainstore (
           CONTAINS(encode_json(OBJECT_NAMES(self)), &quot;}&quot;)
           ) 
    WHERE CONTAINS(encode_json(OBJECT_NAMES(self)), &quot;}&quot;) = true;

If you want remove "}" from field name

UPDATE mainstore AS m
SET m = OBJECT REPLACE(n,&quot;}&quot;,&quot;&quot;):v FOR n:v IN m END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), &quot;}&quot;) = true;

If you want remove all the fields that contain "}" from document

UPDATE mainstore AS m
SET m = OBJECT n:v FOR n:v IN m  WHEN POSITION(n,&quot;}&quot;) &lt; 0 END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), &quot;}&quot;) = true;

If you have lot of documents things can be slow. If using EE use eventing and update them. You can find a examples here https://docs.couchbase.com/server/current/eventing/eventing-examples.html

huangapple
  • 本文由 发表于 2023年5月25日 15:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定