Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep existing data in couchbase and only update the new data without overwriting

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.

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

Any suggestions?

like image 632
ramu Avatar asked Oct 21 '22 06:10

ramu


1 Answers

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-clause:

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

update-for:

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

unset-clause:

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

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

namespace-name:keyspace-name.

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

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

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

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: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.

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

RBAC Privileges

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.

For example,

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

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"
        }
    ]

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
        }
    ]

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": "[email protected]",
                "fname": "Dave",
                "hobbies": [
                    "golf",
                    "surfing"
                ],
                "lname": "Smith",
                "relation": "friend",
                "title": "Mr.",
                "type": "contact"
            }
        }
    ]  

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.

Example

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;
like image 58
frank-dspeed Avatar answered Oct 25 '22 18:10

frank-dspeed