couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <>
Subject Re: map reduca with object as key
Date Thu, 18 Aug 2011 05:21:56 GMT
The order in which your keys are stored is not guaranteed by all JSON
parser/serializers. Erlang has the distinction of being one of the few
that does. This means that in Erlang these two objects are *not*

{"a": 1, "b": 2}
{"b": 2, "a": 1}

Because Erlang maintains order, our internal collation is based on
this. As in, since the order is maintained, in the above example we
compare the first key in left-to-right evaluation order. Ie, for the
first object, the "a" key is used in sorting before the "b" key. For
the second object it is the opposite because "b" occurs first in the
parsing of that object.  Thus when sorting "a" is < "b" hence the

What was most likely happening is that your JSON serializer was
changing the order of keys when it sent it to the server. You can
check this by hand using cURL.

Arrays on the other hand have an unambiguous ordering and hence an
unambiguous sort order. This is why you should always use arrays for
compound keys.

Paul Davis

On Thu, Aug 18, 2011 at 12:13 AM, Heiko Henning <> wrote:
> Thanks for antswer,  it work.
> Why store and get  only work with array, but not with object?
> Ok i have now a solution but it think it's odd.
> *Work:*
> emit([doc.lom_id, doc.user], doc.value);
> emit([doc.lom_id, 'all'],    doc.value);
> /testdb/_design/rating/_view/get_rating?group=true&key=[1337, "all"]
> *Dont work:*
> emit({lom_id:doc.lom_id, user:doc.user}, doc.value);
> emit({lom_id:doc.lom_id, user:'all'},    doc.value);
> key = {"lom_id":"1337","user":"all"}
> /testdb/_design/rating/_view/get_rating?group=true&key={%22lom_id%22%3A%221337%22%2C%22user%22%3A%22all%22}
> Regards Heiko
> Am 17.08.2011 18:40, schrieb Jens Alfke:
>> On Aug 17, 2011, at 12:09 AM, Heiko Henning wrote:
>> Is there a possibility to have key as Object?
>> Or have i to do thinks like:
>> emit(doc.lom_id+'___'+user:doc.user, doc.value);
>> emit(doc.lom_id+'___all', doc.value);
>> Usually people use arrays, not objects, as composite keys. Keys are
>> sorted, after all, and it’s kind of unclear how to sort objects since the
>> key-value pairs have no intrinsic order.
>> So try something like emit([doc.lom_id, user:doc.user], doc.value);
>> —Jens

View raw message