couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zachary Zolton <zachary.zol...@gmail.com>
Subject Re: View collation by one key and sorting by another key
Date Fri, 05 Aug 2011 22:54:31 GMT
If you want oranges sold over time, try this view:

map: function(doc) {
  var dateOfPurchase = trimOffTimePart(doc.dateOfPurchase);
  emit([doc.itemId, dateOfPurchase], null);
}

reduce: _count

Now, query to get the counts:

http://couch/db/_design/app/_view/items-sold-by-date?group=true&startkey=["oranges"]&endkey=["oranges",{}]

(You'll have to URL encode those keys, too...)

That should result in a row with a count of oranges sold for each day.
More like what you were looking for? (^_^

--Zach


On Fri, Aug 5, 2011 at 5:34 PM, Matthew Slade
<matthew.slade@moonlight42.com> wrote:
> Hi Randall
>
> Thanks, I'm aware of the ability to split out the date into multiple keys
> for easier collation. However that's not really the issue here. My problem
> is with the additional level of sorting that might be required to make the
> reduced data meaningful. ie I want to know only about oranges and not other
> fruit in my particular date time collation.
>
> Matthew
>
> On Sat, Aug 6, 2011 at 12:19 AM, Randall Leeds <randall.leeds@gmail.com>wrote:
>
>> On Fri, Aug 5, 2011 at 15:00, Matthew Slade
>> <matthew.slade@moonlight42.com> wrote:
>> > Hi Zach (and everyone else)
>> >
>> > Thanks for the info. Unfortunately my game is already big and the
>> proposed
>> > solution wouldn't really be suitable although _lists seem interesting and
>> > I'll definitely investigate them. I do find this quite a limitation to
>> > couchdb especially when one is working a lot with dated documents.
>> >
>> > Another question I have in a similar vein:
>> >
>> > Given a set of documents being created for transaction for the purchase
>> of
>> > fruit for example.
>> >
>> > * {
>> > "_id":"uniqueOrderId",
>> > "dateOfPurchase": "2011-07-06T10:24:52",
>> > "itemId": "oranges"
>> > }*
>> >
>> > Could one collate the view entries by the date and then reduce based off
>> of
>> > part of the value. ie How could I easily answer the question "*How many
>> > oranges were sold on Wednesday*?" efficiently with potentially huge
>> numbers
>> > of transaction documents in my database.
>>
>> You should separate the key you emit into an array of the date pieces
>> like [YYYY, MM, DD, ...].
>> Then you can use ?group_level=X with your reduce query to get hourly,
>> daily, monthly, etc summaries.
>>
>> -Randall
>

Mime
View raw message