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.

Thanks,

Hello,

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?

Regards
Tug
@tgrall

Perfect thanks Tug. I’m still in the mindset of “get everything you need in one query” rather than returning a list of ids and then a separate get().

Am I right in thinking then that the results of the map function would then be used to call get_multi()?

How would this work when a sort order is required, because the key would be a composite key? Would I need to return the document id as the value, and send the list of values to get_multi?