如何将现有数据保留在Couchbase中,仅更新新数据而不覆盖 [英] How do I keep existing data in couchbase and only update the new data without overwriting

查看:102
本文介绍了如何将现有数据保留在Couchbase中,仅更新新数据而不覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我在一个存储桶下创建了一些记录/文档,并且用户在RDBMS中仅更新了10列中的一列,所以我试图仅发送该列中的数据并将其在沙发上更新。但是问题是,couchbase会覆盖整个记录,并在其余列中放入NULL。

So, say I have created some records/documents under a bucket and the user updates only one column out of 10 in the RDBMS, so I am trying to send only that one columns data and update it in couchbase. But the problem is that couchbase is overwriting the entire record and putting NULL`s for the rest of the columns.

一种方法是从现有记录中复制所有数据从Cbase提取数据后,然后在从旧数据库复制数据时覆盖新列。但这似乎不是最佳方法

One approach is to copy all the data from the exisiting record after fetching it from Cbase, and then overwriting the new column while copying the data from the old one. But that doesn`t look like a optimal approach

有任何建议吗?

推荐答案

您可以将N1QL更新说明Google用作Couchbase N1QL
UPDATE将已存在的文档替换为更新值。

You can use N1QL update Statments google for Couchbase N1QL UPDATE replaces a document that already exists with updated values.

update:

UPDATE keyspace-ref [use-keys-clause] [set-clause] [unset-clause] [where-clause] [limit-clause] [returning-clause]

设置子句:

SET path = expression [update-for] [ , path = expression [update-for] ]*

更新:

FOR variable (IN | WITHIN) path  (, variable (IN | WITHIN) path)* [WHEN condition ] END  

未设置子句:

UNSET path [update-for] (, path [ update-for ])*  
keyspace-ref: Specifies the keyspace for which to update the document.

您可以通过以下方式在keyspace-name中添加可选的namespace-name:

You can add an optional namespace-name to the keyspace-name in this way:

namespace-name:keyspace-name.

use-keys-clause:指定要更新的数据项的密钥。可选的。键可以是任何表达式。

use-keys-clause:Specifies the keys of the data items to be updated. Optional. Keys can be any expression.

set-clause:指定要更改的属性的值。

set-clause:Specifies the value for an attribute to be changed.

unset-clause:从文档中删除指定的属性。

unset-clause: Removes the specified attribute from the document.

update-for:update for子句使用FOR语句遍历嵌套数组,然后SET或UNSET数组中每个匹配元素的给定属性。

update-for: The update for clause uses the FOR statement to iterate over a nested array and SET or UNSET the given attribute for every matching element in the array.

where-clause:指定要更新数据所需满足的条件。

where-clause:Specifies the condition that needs to be met for data to be updated. Optional.

限制子句:指定可更新的最大对象数。此子句的上限必须为非负整数。

limit-clause:Specifies the greatest number of objects that can be updated. This clause must have a non-negative integer as its upper bound. Optional.

返回子句:返回您在result_expression中指定的更新数据。

returning-clause:Returns the data you updated as specified in the result_expression.

RBAC特权

执行UPDATE语句的用户必须对目标键空间具有查询更新特权。如果该语句具有需要读取数据的任何子句(例如SELECT子句或RETURNING子句),则还需要对各个子句中引用的键空间具有查询选择特权。有关用户角色的更多详细信息,请参见授权。

User executing the UPDATE statement must have the Query Update privilege on the target keyspace. If the statement has any clauses that needs data read, such as SELECT clause, or RETURNING clause, then Query Select privilege is also required on the keyspaces referred in the respective clauses. For more details about user roles, see Authorization.

例如,

要执行以下语句,用户必须对旅行样本具有查询更新特权。

To execute the following statement, user must have the Query Update privilege on travel-sample.

UPDATE `travel-sample` SET foo = 5

要执行以下语句,用户必须具有查询更新特权在旅行样本上,在啤酒样本上具有查询选择特权。

To execute the following statement, user must have the Query Update privilege on the travel-sample and Query Select privilege on beer-sample.

UPDATE `travel-sample` 
SET foo = 9 
WHERE city = (SELECT raw city FROM `beer-sample` WHERE type = "brewery"
To execute the following statement, user must have the Query Update privilege on `travel-sample` and Query Select privilege on `travel-sample`.

UPDATE `travel-sample` 
SET city = "San Francisco" 
WHERE lower(city) = "sanfrancisco" 
RETURNING *
Example

以下语句更改了产品的类型 t, odwalla-juice1转换为 product-juice。

The following statement changes the "type" of the product, "odwalla-juice1" to "product-juice".

UPDATE product USE KEYS "odwalla-juice1" SET type = "product-juice" RETURNING product.type

"results": [
        {
            "type": "product-juice"
        }
    ]

此语句从带有 odwalla的文档的产品键空间中删除 type属性-juice1键。

This statement removes the "type" attribute from the "product" keyspace for the document with the "odwalla-juice1" key.

UPDATE product USE KEYS "odwalla-juice1" UNSET type RETURNING product.*

"results": [
        {
            "productId": "odwalla-juice1",
            "unitPrice": 5.4
        }
    ]

此语句在文档的子级数组中为键 dave取消设置 gender属性。教程键空间。

This statement unsets the "gender" attribute in the "children" array for the document with the key, "dave" in the tutorial keyspace.

UPDATE tutorial t USE KEYS "dave" UNSET c.gender FOR c IN children END RETURNING t

"results": [
        {
            "t": {
                "age": 46,
                "children": [
                    {
                        "age": 17,
                        "fname": "Aiden"
                    },
                    {
                        "age": 2,
                        "fname": "Bill"
                    }
                ],
                "email": "dave@gmail.com",
                "fname": "Dave",
                "hobbies": [
                    "golf",
                    "surfing"
                ],
                "lname": "Smith",
                "relation": "friend",
                "title": "Mr.",
                "type": "contact"
            }
        }
    ]  

从版本4.5.1开始,UPDATE语句具有已改进为SET嵌套数组元素。增强了FOR子句以评估函数和表达式,新语法支持多个嵌套的FOR表达式来访问和更新嵌套数组中的字段。通过链接FOR子句可以支持其他数组级别。

Starting version 4.5.1, the UPDATE statement has been improved to SET nested array elements. The FOR clause is enhanced to evaluate functions and expressions, and the new syntax supports multiple nested FOR expressions to access and update fields in nested arrays. Additional array levels are supported by chaining the FOR clauses.

示例

UPDATE default
    SET i.subitems = ( ARRAY OBJECT_ADD(s, 'new', 'new_value' )
        FOR s IN i.subitems END ) 
            FOR s IN ARRAY_FLATTEN(ARRAY i.subitems 
                FOR i IN items END, 1) END;

这篇关于如何将现有数据保留在Couchbase中,仅更新新数据而不覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆