couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <B.Cand...@pobox.com>
Subject Re: Multiple keys and partial query.
Date Tue, 23 Feb 2010 09:25:58 GMT
On Tue, Feb 23, 2010 at 01:24:44AM +0100, Olivier Le Thanh Duong wrote:
> I basically have a database where document are short simple messages,
> each with fields sender, receiver, text and datetime. Sender and
> Receiver are simple strings identifier and the datetime are array of
> interger. e.g  [2009,12,6,20,5,40]
> I found this format for the datetime in the O'Reilly couchdb book, I
> don't know if that's the prefered format or not but it allow to
> specify only part of the date in requests.
> 
> So first I wanted to get all the message from one person so I first
> created a view like this :
> function(doc) {
>   emit(doc.sender, doc);
> }
> and queried it with ?inclusive_end=true&startkey="name"&endkey="name"
> This works fine.

or in this case just: key="name"

> But then I wanted to have them sorted by date so I added the date to
> my key and put both in an array.
> 
> function(doc) {
>   emit([doc.sender, doc.date], doc);
> }
> 
> Which give me keys like : ["Sender",[2009,11,16,16,34,49]]
> 
> If I do a query specifying whole key it works fine e.g
> ?inclusive_end=true&endkey=["Sender",[2011, 0, 0, 0,
> 0,0]]&startkey=["Sender",[0, 0, 0, 0, 0,0]]
> But CouchDb is supposed to be able to retrieve result even when only
> the start of a key is present, so for example I can query specifying
> only ["Sender",[2009]] and it works.
> However if I do a request with only ["Sender"] or ["Sender",[]] I
> don't get any result.

You won't. Try startkey=["Sender"]&endkey=["Sender",{}]

The collation rules mean that an object like {} sorts after any array
value. See http://wiki.apache.org/couchdb/View_collation

Only grouped queries let you specify just a prefix of an array. Off the top
of my head it's something like this (untested):

   group=true&group_level=1&key=["Sender"]

But then you'll get the reduce value across all rows with that Sender, not
the individual rows with that Sender.

HTH,

Brian.

Mime
View raw message