Python sdk add new attribute to existing [RESOLVED]

anyone can provide sample python code snippet that would do the following functionality?

  1. I would like to be able to add some attribute on an existing document
  2. edit an existing attribute
    The values required on the “update” will be provided by user as parameter

my doc looks like this

{
“type”: “address”,
“DEV1”: {
“port”: “43000”,
“host”: “host1.bigip.com
},
“PT2”: {
“port”: “42000”,
“host”: “host2.bigip.com
},
“ST2”: {
“port”: “52000”,
“host”: “host3.bigip.com
}
}

You can do this using the subdocument API!

import couchbase.subdocument as SD


bucket.mutate_in('docid', SD.insert('DEV2', {'port': 5000, 'host': 'host5.bigip.com'}), SD.replace('DEV1.port', '4323'))

The best thing about this API is that it’s actually done atomically on the server.

thanks! i’ve been exploring this, however i am getting No mudule exception
when trying to import subdocument

i got a supported version of libcouchbase

$ cbc version
cbc:
Runtime: Version=2.7.4, Changeset=502c6716b24581a258fcb8920532a20a53e81d6b
Headers: Version=2.6.0, Changeset=1bd30c6168cb7b5d4961910ead2a6bb84b5b30f3
IO: Default=libevent, Current=libevent
SSL: … SUPPORTED

Python 2.7.9 (default, Jun 2 2016, 16:37:17)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import couchbase

import couchbase.subdocument
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named subdocument

Upgrade your python client as well.
2.6.0 seems too old as well (I’d need to check).

Your cbc version output suggests you have multiple installs of libcouchbase. Perhaps check that you don’t have any lingering installs in /usr/local or similar.

Yes, i do have installed 2 CBC versions(I don’t have root so the install dirs are customized.)

My python is at 2.7.9

$ python
Python 2.7.9 (default, Jun 2 2016, 16:37:17)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2

Your cbc is not using the same runtime it was compiled. The output shows it’s using 2.7.4 headers against a 2.6.0 runtime. This isn’t an error per se, but something to be aware of.

The couchbase.subdocument module was added in our Python client version 2.0.8 - a bit over a year ago.

Thx, (non-root- problems i know :slight_smile: ) i could clean that up.

is it only the subdocument that is capable of doing the “updates” ?

i couldn’t find anything on the web about this error

Yes, the subdocument API is designed specifically for that purpose. You can use traditional full-document upsert/insert/replace etc. if you want to change the contents of the entire document.

The ImportError you’re getting is simply because the module doesn’t exist in the Python client you’re using. There may be other reasons - but those are all related to how you’ve set up your Python project.

Mark, thx. Fixed my python install and upgraded couchbase client. It works wonderful