couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Newson <robert.new...@gmail.com>
Subject Re: First doc in a group?
Date Thu, 11 Nov 2010 22:15:10 GMT
The purpose of reduce is to summarize or aggregate.

Matthew's suggestion is correct. Emit keys such that your date sorts
by date then use descending=true to get the latest item of interest by
date. Use limit=1 to ensure you get one result.

To expand it more fully;

map: emit([doc.user, doc.timestamp], null);

then query with;

startkey=["user I want",{}]
endkey=["user I want"]
descending=true
limit=1

B.

On Thu, Nov 11, 2010 at 9:45 PM, Cory Zue <czue@dimagi.com> wrote:
> You could emit the users as keys, and in your reduce function just
> return the latest by date.
>
> On Thu, Nov 11, 2010 at 4:35 PM, Mike Bannister <mikebannister@gmail.com> wrote:
>> Yeah, I'm trying to get the latest document for each user in one query
>> rather than one query per user.
>>
>> On Thu, Nov 11, 2010 at 4:11 PM, Matthew Woodward <matt@mattwoodward.com>wrote:
>>
>>> On Thu, Nov 11, 2010 at 12:59 PM, Mike Bannister <mikebannister@gmail.com
>>> >wrote:
>>>
>>> > I have a set of documents that are related to users in my system and each
>>> > document has a timestamp. What's the CouchDB way of getting the latest
>>> > document for each user?
>>>
>>>
>>> I'm pretty new to CouchDB as well so take this for what it's worth--in one
>>> of our databases we needed to get the most recent document based on a
>>> timestamp, so we just have a view called "byDate" with the key being the
>>> timestamp, and then we just call the view like this:
>>>
>>> http://server/database/_design/designname/_view/byDate?descending=true&limit=1
>>>
>>> Not sure that's quite what you're after and might not address the user part
>>> of what you're doing, but hope that helps a bit.
>>>
>>> --
>>> 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
>>>
>>
>

Mime
View raw message