Dimitar Rusev
Dimitar Rusev

Reputation: 90

Couchbase - updating object inside a document

If I have the following document for example: { “make”:“BMW”, “make1”:“AUDI” }

When I do an UPDATE query UPDATE Translations SET make2 = “MERCEDES” WHERE META().id = “CARS” , there is no problem with the query and make2 is added to the document.

When I do an UPDATE query UPDATE Translations SET make2.MODEL = “CLS”, make2.MODIFICATION = “500” WHERE META().id = "CARS" The query returns “sucess”, but nothing is added. If make2 already exists everything is updated as expected, the problem only appears if the object does not exist.

Upvotes: 2

Views: 1310

Answers (1)

Jeff Kurtz
Jeff Kurtz

Reputation: 661

You can only update the nested properties if the parent property is a JSON Object. If the parent property is set to a string, the update will not succeed, contrary to what you stated.

This will work:

UPDATE Translations USE KEYS "CARS" 
    SET make2 = {};
UPDATE Translations USE KEYS "CARS" 
    SET make2.MODEL = "CLS", d.make7.MODIFICATION = "500";

Or you can do this in one line:

UPDATE Translations USE KEYS "CARS" 
    SET make2 = {}, make2.MODEL = "CLS", d.make7.MODIFICATION = "500";

Upvotes: 1

Related Questions