Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 17574C3F5 for ; Tue, 12 Mar 2013 01:22:47 +0000 (UTC) Received: (qmail 26854 invoked by uid 500); 12 Mar 2013 01:22:45 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 26831 invoked by uid 500); 12 Mar 2013 01:22:45 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 26821 invoked by uid 99); 12 Mar 2013 01:22:45 -0000 Received: from minotaur.apache.org (HELO minotaur.apache.org) (140.211.11.9) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Mar 2013 01:22:45 +0000 Received: from localhost (HELO mail-la0-f51.google.com) (127.0.0.1) (smtp-auth username rnewson, mechanism plain) by minotaur.apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Mar 2013 01:22:45 +0000 Received: by mail-la0-f51.google.com with SMTP id fo13so4645920lab.38 for ; Mon, 11 Mar 2013 18:22:43 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.152.132.138 with SMTP id ou10mr12188786lab.56.1363051363286; Mon, 11 Mar 2013 18:22:43 -0700 (PDT) Received: by 10.112.25.201 with HTTP; Mon, 11 Mar 2013 18:22:43 -0700 (PDT) In-Reply-To: References: Date: Mon, 11 Mar 2013 20:22:43 -0500 Message-ID: Subject: Re: Sort a query by a time-senstive criteria From: Robert Newson To: "user@couchdb.apache.org" Content-Type: text/plain; charset=ISO-8859-1 couchdb views can only be sorted by their key. On 11 March 2013 20:20, Traun Leyden wrote: > Thanks! I'll follow up w/ you off-list regarding the Cloudant-specific > approach. > > I actually need to make this work on TouchDB, so still interested to hear > if anyone can recommend a good solution using pure a CouchDB approach. > > On Mon, Mar 11, 2013 at 5:09 PM, Robert Newson wrote: > >> Since you're on Cloudant (probably shouldn't post to the couchdb user list >> tbh); >> >> index('purchase_date', purchase.time); // assuming purchase.time is an >> epoch integer. >> >> and use ?q=purchase_date:[earliest TO latest]&sort="purchase_date" >> >> where earliest and latest are replaced by the appropriate number of >> millis since epoch. >> >> B. >> >> >> On 11 March 2013 18:49, Traun Leyden wrote: >> > I want to be able to sort a list of users by how much they've spent in >> the >> > last 6 months. >> > >> > The document has a structure of: >> > >> > { >> > "type":"user", >> > "purchases": [ {"type":"purchase","time":"2012-11-05 >> > 17:37:52","spent":50}, ...] >> > } >> > >> > Here's how I'm planning on writing the index: >> > >> > var cutoffDate = new Date(); >> > cutoffDate.setHours(0,0,0,0); >> > cutoffDate.setMonth(cutoffDate.getMonth() - 6); >> > var total6m = 0; >> > doc.purchases.forEach(function(purchase) { >> > if((new Date(purchase.time)) > cutoffDate)) { >> > spent6m += purchase.spent; >> > } >> > } >> > index('spent6m', spent6m, {'store':'yes'}); >> > >> > The disadvantage to this approach is that the indexed data quickly gets >> > stale, because "new Date()" is only evaluated at index time and not when >> > the query is run. Essentially it breaks the CouchDB rule of not having >> > the view depend on external data. >> > >> > As a workaround, I'm thinking of adding a new field to the user object to >> > store the purchase amount for the last 6 months, as well as adding a >> > nightly process to update that field to keep the value from getting stale >> > as time progresses. >> > >> > Is this the recommended approach or is there a cleaner way? >> > >> > Thanks, >> > Traun >>