1
votes

any one can help me about this query how can Parent-document update using couchbase N1ql query

{
    "created_at": "2020-03-26T15:50:12.318Z",

    "created_by": 1,
    "deleted_at": "",
    "frm21_submit": null,
    "id": "6cb51519-7c6b-499d-8a8d-3c85658605fc",
    "machine_category_id": [
        "7a2eb767-faca-4762-b65b-2db9e1992c82",
        "259a4bcc-feb5-4d98-88c5-b331316e19be"
    ],

    "main_parts": [

        {
            "data": {
                "deleted_at": "777",
                "frm21_submit": null,
                "manufacturing_date": "03/31/2020",
                "model": "234234234",
                "photo": null,
                "serial_number": "324324234",
                "updated_at": "2020-03-31T14:11:48.909Z"
            },
            "id": "66354d7c-4769-4f3b-914e-bb841191e323"
        }
    ]

}

i want to update parent-document only all field are dynamic i can not selected field. like

update machine set "created_by"= 1, "machine_category_id"= [
    "7a2eb767-faca-4762-b65b-2db9e1992c82",
    "259a4bcc-feb5-4d98-88c5-b331316e19be"
  ]  Where id=1

how to dynamically update parent document only with safe child or sub document nothing lose data.

{

    "created_at": "2020-03-26T15:50:12.318Z",

    "created_by": 1,

    "deleted_at": "",

    "frm21_submit": null,

    "id": "6cb51519-7c6b-499d-8a8d-3c85658605fc",

    "machine_category_id": [
        "7a2eb767-faca-4762-b65b-2db9e1992c82",
        "259a4bcc-feb5-4d98-88c5-b331316e19be"
    ]

}
1
I don't quite understand the problem. Why doesn't an UPDATE work for you? - Matthew Groves
in full document have many more sub document but i want update parent document only . but i have a one problem field are not static so i can not write update query with static field so is it possible . thanks - utpal poulik
Your question is not clear. In JOSN you can store value in field. If you don't know field how do you modify the value. If you want to overwrite/add fields on top use UPDATE default AS d SET d = OBJECT_CONCAT(d, {"created_by": 2,"newfiled":"xyz") WHERE .....; This overwrite field if exist and if not it adds. - vsr
Thank you so much. its working and problem solved - utpal poulik

1 Answers

0
votes

The problem in your update is that it is referring to an attribute called "id" which I guess that is the key of the document. To access the document id you have to call meta().id

update default set created_by= 1, machine_category_id= [
"machine",
"asdasd"
]  Where meta().id = "1"

Note that the document id is a string.