Adding new object to existing document using N1QL

n1ql

#1

First document resultsobject is null.
I want to add some fields under resultsobject …mentioned below second one.
Kindly help me in providing the N1QL for this.
{
“BUcketName”: {
“Comment”: “asdfsdaf”,
“resultsobject”: null,
“tcid”: “asdf”,
“type”: “dsaf”
}
}

Need to update like below.

{
“BUcketName”: {
“Comment”: “asdfsdaf”,
“resultsobject”: {
“result”: {
“code” : “sdafsda”,
“descripton” : “sucess”
}
}

  "tcid": "asdf",
  "type": "dsaf"
}

}


#2
UPDATE default AS d 
SET d.resultsobject =  { "result": {"code" : "sdafsda","descripton" : "sucess"}}
WHERE .....;

#3

I tired doing that …everything going an string instead of an object like below

{
“BUcketName”: {
“Comment”: “asdfsdaf”,
“resultsobject”: “{“result”: {“code” : “sdafsda”,“descripton” : “sucess”}”,
}
“tcid”: “asdf”,
“type”: “dsaf”
}
}

Reply

  • created

9h

  • [

last reply

1h
](Adding new object to existing document using N1QL)

  • 1

reply

  • 14

views

  • 2

users

vsr1

1h

UPDATE default AS d 
SET d.resultsobject =  { "result": {"code" : "sdafsda","descripton" : "sucess"}}
WHERE .....;

Reply

Bookmark Share Flag Reply

Watching

You will receive notifications because you created this topic.

Suggested Topics

Topic Replies Views Activity
Stored procedures in N1QL 2

N1QL|16|4.1k|Jun 5|
|Sorting data using Order By created or last modified time

N1QL

query n1ql index|1|15|36m|
|Very slow performance on ANSI JOIN from different bucket

N1QL

n1ql|1|15|1h|
|Error creating index, possible bug?

N1QL

index|3|40|18h|
|Couchbase query execution taking too much time

N1QL

query|2|26|1d|

There is 1 unread and 15 new topics remaining, or browse other topics in N1QL

Adding new object to existing document using N1QL


#4

You might have passed as string instead of Object.

INSERT INTO default VALUES ("k01", { "Comment": "asdfsdaf", "resultsobject": null, "tcid": "asdf", "type": "dsaf" });
SELECT * FROM default USE KEYS "k01";
UPDATE default AS d USE KEYS "k01" SET d.resultsobject =  { "result": {"code" : "sdafsda","descripton" : "sucess"}};
SELECT * FROM default USE KEYS "k01";

cbq> INSERT INTO default VALUES ("k01", { "Comment": "asdfsdaf", "resultsobject": null, "tcid": "asdf", "type": "dsaf" });
{
    "requestID": "652525e1-4363-4f79-9e3e-078d3823c6fe",
    "signature": null,
    "results": [
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "1.797609ms",
        "executionTime": "1.680491ms",
        "resultCount": 0,
        "resultSize": 0,
        "mutationCount": 1
    }
}
cbq> SELECT * FROM default USE KEYS "k01";
{
    "requestID": "0f6cc7d3-c893-47fa-8de7-b87fb4467149",
    "signature": {
        "*": "*"
    },
    "results": [
    {
        "default": {
            "Comment": "asdfsdaf",
            "resultsobject": null,
            "tcid": "asdf",
            "type": "dsaf"
        }
    }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "663.404µs",
        "executionTime": "600.877µs",
        "resultCount": 1,
        "resultSize": 163
    }
}
cbq> UPDATE default AS d USE KEYS "k01" SET d.resultsobject =  { "result": {"code" : "sdafsda","descripton" : "sucess"}};
{
    "requestID": "9a4dc4dd-846e-4fec-8592-8b32cb98dca6",
    "signature": null,
    "results": [
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "1.112692ms",
        "executionTime": "1.060617ms",
        "resultCount": 0,
        "resultSize": 0,
        "mutationCount": 1
    }
}
cbq> SELECT * FROM default USE KEYS "k01";
{
    "requestID": "3f5b0331-d3b0-4bb5-b294-a6ca7fa0b6de",
    "signature": {
        "*": "*"
    },
    "results": [
    {
        "default": {
            "Comment": "asdfsdaf",
            "resultsobject": {
                "result": {
                    "code": "sdafsda",
                    "descripton": "sucess"
                }
            },
            "tcid": "asdf",
            "type": "dsaf"
        }
    }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "1.067177ms",
        "executionTime": "973.349µs",
        "resultCount": 1,
        "resultSize": 302
    }
}

#5

Thanks for details.
It’s working now. earlier I kept everything in quotes …now removed the quotes it’s working.

Writting N1ql in java class to update documents. Actually I have to 10K+ documents hence putting the update query in java class.

			applicationname1 = expectedResult.getExpeectedresponse();
			applicationname2 = expectedResult.getTcnumber();

here applicationname1 have declared as string. and entire object is going as String.

How I have to declare applicationname1 here. Please suggest.

	JsonObject appnames = JsonObject.create().put("appname", applicationname1)
			.put("functionname", applicationname2);
	String statement = "update CPGAutomation set EXPECTEDRESULT = $appname where tcid = '001'";
	N1qlQuery querytoextractplaceholders = N1qlQuery.parameterized(
			statement, appnames);
	secureBucket.query(querytoextractplaceholders);