You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Olivier Le Thanh Duong <ol...@lethanh.be> on 2010/02/23 01:24:44 UTC

Multiple keys and partial query.

Hello,
I am playing a bit with CouchDB and I ended up with a view with
multiple key and I find a strange behaviour while querying it so I'm
pondering if this is considered normal or if I'm doing thing the wrong
way.

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.

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.

So is this considered normal behaviour? Is it a bug? Is it already
fixed in a latter version ?  (The version of CouchDB I'm using is
0.10.0)
Or am I doing thing the wrong way?

Regards,
Olivier


-- 
Olivier LĂȘ Thanh Duong <ol...@lethanh.be>

Phone : +32485608639 Jabber: olethanh@gmail.com

Re: Multiple keys and partial query.

Posted by Brian Candler <B....@pobox.com>.
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.