英文:
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,"}"") < 0 END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), ""}"") = 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)), "}")
)
WHERE CONTAINS(encode_json(OBJECT_NAMES(self)), "}") = true;
If you want remove "}" from field name
UPDATE mainstore AS m
SET m = OBJECT REPLACE(n,"}",""):v FOR n:v IN m END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), "}") = 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,"}") < 0 END
WHERE CONTAINS(encode_json(OBJECT_NAMES(m)), "}") = 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论