Create object using n1ql update

I have a test document

{
   a : 1
}

How can I convert it to

{
   a : 1,
   b:{
      c:{
         d : 2,
      }
   },
   e:[
      {
         f : 3
      },{
         g : 4
      }
   ]
}

The following n1ql query don`t do that

UPDATE my_bucket USE KEYS "test" SET b.c.d=2,e=[{f:3},{g:4}]

Hi @socketman2016, for single-doc operations you should use the KV API over N1QL - it will be much faster and you can do this kind of operation very easily. Docs are here - you can change to whatever language you’re using with the top-right combo.

1 Like

Without parent field an object you can’t update sub field. i.e There is no b or b.c so you can’t update b.c.d

Try one of these

UPDATE default USE KEYS "test" SET b = {}, b.c = {}, b.c.d=2,e=[{"f":3},{"g":4}];
UPDATE default USE KEYS "test" SET b = {"c":{"d":2}},e=[{"f":3},{"g":4}];
1 Like

Why much faster? I know it is faster because there is no need to parse n1ql query , but as I use USE KEYS why you told it is much faster? how much? (just as a thumb of rule 100%?200%? )

It’s hard to estimate how much faster it will be, as it will depend on your exact network and topology. But the KV operation can go directly from the client to the correct vbucket and involves no parsing, so it’s the fastest possible approach.

1 Like