View Map to return Key/Value pairs as Value

Is it possible to return Key/Value pairs in emit() using cb.query() in a similar way to the return value of cb.get() ?

cb.get() returns the complete object, however a cb.query() returns an indexed array as per the array ([ doc.x, doc.y , doc.z]) format of the emit() return value.


1 Answer

« Back to question.


The map function has been created to only emit a key/value pair, and your should try to keep it as small as possible.

This is why you see most of the application emitting only a key and nothing else. This is just because the index is on disk and called using a REST API, where in Couchbase when you get the document with a cb.get() operation you use the fast and scalable memcached protocol.

So the good patter is to:
- emit the key in the map function
- do not emit value if you do not need it in a reduce function, you can emit also as value one attribute or an aggregate
- query this index (view) with or without parameters
- this return a list of document id, use this list to get the document using cb.get() operation, that are using memcached protocol and will cache the document automatically.

Does it make sense?