0

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

1 Answers1

2

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
    });
Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
  • How to do this in previous version of CB server like 4.0 – Alex Rodnies Sep 01 '16 at 13:33
  • In CB 4.1, N1QL added mutative statements, so you should be able to do an UPDATE similar to `UPDATE b SET media = ARRAY_APPEND(a, {'mediaId': '...'})`. Before 4.1, you're stuck with getting the full document through the SDK, updating the relevant fields programmatically then re-inserting the updated document (also most probably checking the CAS) – Simon Baslé Sep 01 '16 at 13:38