Search code examples
couchbasesql++couchbase-nodejs-sdk

Can I add new object to the array in couchbase docusing n1ql or node sdk


I have following doc:

{
    "userId":"b447drga851g",
    "media": [
        {
          "mediaId": "stf9-oi6f-kkvt7s-plt6c-iud5",
          "mediaType": "IMAGE",
          "fileName": "sssss.jpeg",
          "device": "LENOVO A6000",
          "format": "jpg",
          "size": "530",
          "resolution": "1900*1200",
          "isExifData": true,
        },{
          "mediaId": "stf9-oi6f-kkvt7s-plt6c-iud5",
          "mediaType": "IMAGE",
          "fileName": "sssss.jpeg",
          "device": "LENOVO A6000",
          "format": "jpg",
          "size": "530",
          "resolution": "1900*1200",
          "isExifData": true,
        }
      ]
}

Now i have another media object so can i update this doc and add new media object to the media array.

currently i am fetching whole doc and pushing new object via node.js code so is there any way to do that in couchbase using nickel or sdk


Solution

  • If you have Couchabse Server 4.5, you can use the "sub-document" feature through the nodejs SDK to do just that.

    something like:

    bucket.mutateIn('userKey')
        .arrayAppend('media',
          {"mediaId": "stf9-oi6f-kkvt7s-plt6c-iud5",
          "mediaType": "IMAGE",
          "fileName": "sssss.jpeg",
          "device": "LENOVO A6000",
          "format": "jpg",
          "size": "530",
          "resolution": "1900*1200",
          "isExifData": true},
          false)
        .execute(function(err, result) {
            //check result and/or error
        });