Lookup documents

Couch noob :smile:

  • I see that a counter document (assessed via the atomic operations) stored a Doc ID along with just a value. Could I do the same thing for a Lookup document?

Im using the Python SDK, and it won’t let me do that since the document fails the JSON standard. I end up having to create a document that looks like this (with just one key-value):

{
“base” : “user::blah::blah”
}

Or, is there something I’m missing after watching the CB101 series regarding creating Lookup docs?

Thanks!

Python should allow simple numbers as counters:

mnunberg@mbp15 ~ $ python
Python 2.7.9 (default, Jan 29 2015, 06:27:40) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from couchbase.bucket import Bucket
>>> cb = Bucket('couchbase://192.168.72.101/default')
>>> cb.counter('counterKey', delta=1, initial=110)
ValueResult<RC=0x0, Key=u'counterKey', Value=110L, CAS=0x50d14b962b27d713, Flags=0x0>
>>> cb.get('counterKey')
ValueResult<RC=0x0, Key=u'counterKey', Value=110, CAS=0x50d14b962b27d713, Flags=0x0>
>>> 

Also, the default format is FMT_JSON, but you can also set the format= keyword argument to something like FMT_BYTES, FMT_UTF8 etc. if you don’t want your value stored as JSON (though in this case, an ASCII number is already JSON)

@mnunberg could I do something like:

cb.insert(docID, ‘38y492y49823y498888abh’)

would I then have to specify the format as FMT_UTF8?

like so:

cb.insert(docID, ‘38y492y49823y498888abh’, format=FMT_UTF8) ?

Not really. The basic types in python (list, tuple, int, unicode or str) can all be represented as JSON, so there is no need to store them as anything else. It might be more efficient for some performance critical applications to skip the step of JSON encoding/decoding if all they are storing are simple strings (and here is where things like FMT_UTF8 come into play).

Some objects (specifically, those which are raw bytes – they don’t have an encoding and cannot be encoded as UTF-8) cannot be represented as JSON.

In your case, you have a simple string, so I don’t see what’s wrong with the default format. Are you getting an exception anywhere?

I was trying to do it as UTF-8 mainly for performance reasons - to prevent encoding/decoding.

I was trying to store session tokens and use that for user validation. Those are usually just key value references. Hence I was trying to avoid the JSON encoding/decoding.

Yes, then FMT_UTF8 or FMT_BYTES (if you want to store your data as binary data) should do the trick!