Access the values from a get

I know that I can use a get to get a specific field of a document BUT what if I use a get to get the entire document but then want to access a specific field?

so I do a get and then print the rv.value
which displays below:
{‘mName’: ‘’, ‘addr1’: ‘130333 575 Proudfoot Lane’, ‘lName’: ‘Bonilla M’, ‘city’: ‘London’, ‘fName’: ‘Guillermo’}

is it now possible to get the value of fName from the rv object?

I know that I can do

rv = cb.lookup_in(custId, SD.get(‘fName’))

but I would like to retrieve the entire document and then look at several fields, I would think it should be possible to get the entire document and the extract whatever fields I want, the document seems to hint that this is possible but I cannot seem to figure out how. Any help is appreciated.

dougc

Hi @dougc,

The value attribute of the result is (as the name suggests) the value of the document.

This is stored as a native python dictionary (assuming that the document is JSON) and so all of the fields can be accessed in the same way as any normal dictionary.

To take your example:

>>> rv = bkt.get('test')
>>> print(rv)
ValueResult<rc=0x0, key=u'test', value={u'lName': u'Bonilla M', u'mName': u'', u'city': u'London', u'fName': u'Guillermo', u'addr1': u'130333 575 Proudfoot Lane'}, cas=0x1520bcb884ba0000, flags=0x2000006>
>>> city = rv.value['city']
>>> city
u'London'
>>> f_name = rv.value['fName']
>>> f_name
u'Guillermo'

This is a useful concept as it also allows you to mutate the document in any way that you want to, for example if Guillermo moved to Paris:

>>> rv.value['city'] = 'Paris'
>>> rv
ValueResult<rc=0x0, key=u'test', value={u'lName': u'Bonilla M', u'mName': u'', u'city': 'Paris', u'fName': u'Guillermo', u'addr1': u'130333 575 Proudfoot Lane'}, cas=0x1520bcb884ba0000, flags=0x2000006>
>>> bkt.replace(rv.key, rv.value)
OperationResult<rc=0x0, key=u'test', cas=0x1520bcf2fd170000>
>>> new_rv = bkt.get('test')
>>> new_rv
ValueResult<rc=0x0, key=u'test', value={u'lName': u'Bonilla M', u'mName': u'', u'addr1': u'130333 575 Proudfoot Lane', u'fName': u'Guillermo', u'city': u'Paris'}, cas=0x1520bcf2fd170000, flags=0x2000000>
1 Like

that worked/helped thank you very much

@matt.carabine

How does this work fo a NQL query.
eg
query_string = “select META().id as metaid, t.* from ice_us t where META().id !=‘sdfdsf’ limit 10”
q = N1QLQuery(query_string)

for eachrow in cb.n1ql_query(q):
docid=eachrow.value[‘metaid’]
print (docid)

Traceback (most recent call last):
File “C:/Users/fakj/PycharmProjects/COUCHBASE/couchbase_lab_load.py”, line 33, in
docid=eachrow.value[‘metaid’]
AttributeError: ‘dict’ object has no attribute ‘value’