You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Matthew Woodward <ma...@mattwoodward.com> on 2011/01/25 01:00:49 UTC

Start and End Key with Map as Part of Key

This is actually from another thread but since it might have gotten buried
by the end (and was a bit of a change in topic) I hope folks don't mind the
"repost."

I have the following map function:
function(doc) {
  if (doc.office != '' && doc.witnesses != '[]') {
    doc.witnesses.forEach(function(witness) {
      if (witness.firstName != '' && witness.lastName != '') {
        var wit = {};
        wit.firstName = witness.firstName;
        wit.lastName = witness.lastName;
        emit([doc.office, wit], null);
      }
    });
  }
}

With a reduce and ?group=true this outputs things like:
{"key":["Office1",{"firstName":"Francine","lastName":"Smith"}],"value":true},
{"key":["Office2",{"firstName":"Stan","lastName":"Smith"}],"value":true}

In other instances where my key is an array and the second value is a string
(let's use a date string as an example), I do something like this:
http://server/database/_design/foo/_view/bar?group=true&startkey=[
"Baz","01/01/2011"],&endkey=["Baz",{}]

That will give me all results with "Baz" in the first array element,
starting with 01/01/2011, and then all the rest of results for Baz
regardless of what the date is via the {} in the endkey.

In the case of this data, however, if I want to get back all witnesses for a
specific office this doesn't work:
http://server/database/_design/witnesses/_view/byOffice?group=true&startkey=[
"Office1"]&endkey=["Office1",{}]

That yields 0 rows. If I do this, it works in terms of *starting* with the
correct office, but I can't figure out the correct endkey to get it to only
show witnesses for that particular office:
http://server/database/_design/witnesses/_view/byOffice?group=true&startkey=[
"Office1"]

Is this because the second element of my array key isn't a string?

Thanks!
-- 
Matthew Woodward
matt@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Re: Start and End Key with Map as Part of Key

Posted by Matthew Woodward <ma...@mattwoodward.com>.
On Thu, Jan 27, 2011 at 8:08 AM, Jan Lehnardt <ja...@apache.org> wrote:

> See http://wiki.apache.org/couchdb/View_collation
>
> {} is usually used because it sorts after all strings and numbers. It does
> however, sort before all other non-empty objects.
>
> You've got to pick and endkey that sorts after your "largest" key object.
> e.g. {"firstName":"ZZZZZZZZZZ","lastName":"ZZZZZZZZZZ"} (This is off-hand,
> maybe there's a smarter way)
>

Thanks! I'll give that a shot.

-- 
Matthew Woodward
matt@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Re: Start and End Key with Map as Part of Key

Posted by "Eli Stevens (Gmail)" <wi...@gmail.com>.
Is there a reason your key isn't emit([doc.office, witness.lastName,
witness.firstName]) ?  Also, using "\uffff" or similar might be better than
"ZZZ".

Eli

On Mon, Jan 31, 2011 at 6:28 AM, Matthew Woodward <ma...@mattwoodward.com>wrote:

> On Thu, Jan 27, 2011 at 8:08 AM, Jan Lehnardt <ja...@apache.org> wrote:
>
> > You've got to pick and endkey that sorts after your "largest" key object.
> > e.g. {"firstName":"ZZZZZZZZZZ","lastName":"ZZZZZZZZZZ"} (This is
> off-hand,
> > maybe there's a smarter way)
> >
>
> Just to follow up on this, finally got a chance to play around with this
> and
> this does indeed do the trick. There may be a smarter way but this is smart
> enough for me since it's working. ;-)
>
> Really appreciate the assistance.
> --
> Matthew Woodward
> matt@mattwoodward.com
> http://blog.mattwoodward.com
> identi.ca / Twitter: @mpwoodward
>
> Please do not send me proprietary file formats such as Word, PowerPoint,
> etc. as attachments.
> http://www.gnu.org/philosophy/no-word-attachments.html
>

Re: Start and End Key with Map as Part of Key

Posted by Matthew Woodward <ma...@mattwoodward.com>.
On Thu, Jan 27, 2011 at 8:08 AM, Jan Lehnardt <ja...@apache.org> wrote:

> You've got to pick and endkey that sorts after your "largest" key object.
> e.g. {"firstName":"ZZZZZZZZZZ","lastName":"ZZZZZZZZZZ"} (This is off-hand,
> maybe there's a smarter way)
>

Just to follow up on this, finally got a chance to play around with this and
this does indeed do the trick. There may be a smarter way but this is smart
enough for me since it's working. ;-)

Really appreciate the assistance.
-- 
Matthew Woodward
matt@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Re: Start and End Key with Map as Part of Key

Posted by Jan Lehnardt <ja...@apache.org>.
On 25 Jan 2011, at 01:00, Matthew Woodward wrote:

> This is actually from another thread but since it might have gotten buried
> by the end (and was a bit of a change in topic) I hope folks don't mind the
> "repost."
> 
> I have the following map function:
> function(doc) {
>  if (doc.office != '' && doc.witnesses != '[]') {
>    doc.witnesses.forEach(function(witness) {
>      if (witness.firstName != '' && witness.lastName != '') {
>        var wit = {};
>        wit.firstName = witness.firstName;
>        wit.lastName = witness.lastName;
>        emit([doc.office, wit], null);
>      }
>    });
>  }
> }
> 
> With a reduce and ?group=true this outputs things like:
> {"key":["Office1",{"firstName":"Francine","lastName":"Smith"}],"value":true},
> {"key":["Office2",{"firstName":"Stan","lastName":"Smith"}],"value":true}
> 
> In other instances where my key is an array and the second value is a string
> (let's use a date string as an example), I do something like this:
> http://server/database/_design/foo/_view/bar?group=true&startkey=[
> "Baz","01/01/2011"],&endkey=["Baz",{}]
> 
> That will give me all results with "Baz" in the first array element,
> starting with 01/01/2011, and then all the rest of results for Baz
> regardless of what the date is via the {} in the endkey.
> 
> In the case of this data, however, if I want to get back all witnesses for a
> specific office this doesn't work:
> http://server/database/_design/witnesses/_view/byOffice?group=true&startkey=[
> "Office1"]&endkey=["Office1",{}]
> 
> That yields 0 rows. If I do this, it works in terms of *starting* with the
> correct office, but I can't figure out the correct endkey to get it to only
> show witnesses for that particular office:
> http://server/database/_design/witnesses/_view/byOffice?group=true&startkey=[
> "Office1"]
> 
> Is this because the second element of my array key isn't a string?

See http://wiki.apache.org/couchdb/View_collation

{} is usually used because it sorts after all strings and numbers. It does however, sort before all other non-empty objects.

You've got to pick and endkey that sorts after your "largest" key object. e.g. {"firstName":"ZZZZZZZZZZ","lastName":"ZZZZZZZZZZ"} (This is off-hand, maybe there's a smarter way)

Cheers
Jan
-- 



> 
> Thanks!
> -- 
> Matthew Woodward
> matt@mattwoodward.com
> http://blog.mattwoodward.com
> identi.ca / Twitter: @mpwoodward
> 
> Please do not send me proprietary file formats such as Word, PowerPoint,
> etc. as attachments.
> http://www.gnu.org/philosophy/no-word-attachments.html