1

I'm trying to implement this bit of code:

bucket.mutate_in('some_doc',
                  SD.array_append('some.array', 'Hello', 'World',
                                  create_parents=True))

Which I'm getting from here: https://developer.couchbase.com/documentation/server/current/sdk/subdocument-operations.html

I'm also using this: https://github.com/couchbase/couchnode

I can't import "subdocument" from couchbase, because I get an error saying it doesn't exist, and when I try to use the mutateIn method (or the mutate_in method) I also get an error saying that mutateIn is not a property of Bucket (which it should be).

I would like to append an item to an array (or create an array if it doesn't exists and then append an item to it) for one of my documents in my couchbase bucket.

Attila
  • 1,097
  • 2
  • 19
  • 45

2 Answers2

1

You probably don't have a new enough version installed. Per the release notes, Sub Document support was GA'd in 2.3.0, while it was added in earlier releases as uncommitted.

What does your package.json or npm list command say about the version you have?

Matt Ingenthron
  • 1,894
  • 14
  • 14
  • In my package.json file: under devDependencies I have `"@types/couchbase": "^2.1.30"` but under dependencies I have `"couchbase": "^2.3.4"` – Attila Jul 04 '17 at 01:54
1

Documentation doesn't seem the easiest to navigate around (and the examples in subdocument page are in Python which you can jump to from the node section without realising!) - I had the same problem, check out: https://developer.couchbase.com/documentation/server/4.6/sdk/nodejs/document-operations.html

Try

import couchbase from 'couchbase';

let cluster = new couchbase.Cluster(config.couchbase.clusterIp);
let couchbaseBucket = cluster.openBucket(config.couchbase.bucket);

couchbaseBucket
  .mutateIn(documentId)
  .arrayAppend('key', value)
  .execute(function(err, fragment){

    if (!err || err.code == couchbase.errors.checkResults) {
        try {
          fragment.contentByIndex(0);
        } catch (e) {
          console.error('Error for index %d: %s', 0, e.message);
        }
    } else {
      console.error('Top-level document error: %j', err);
    }

  });
Sam Taylor
  • 275
  • 3
  • 13