couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wout Mertens <wout.mert...@gmail.com>
Subject Re: searching for something like AND clause
Date Thu, 02 Sep 2010 10:18:23 GMT
Hi Mikhail,

you should write your views so they do not change.

For the expiring dates, that means you need to make the expiration date a key and then you
can query all entries that expire later than a given date.

function(doc) {
   doc.type == 'offer' && emit (doc.expires,doc);
}

Since you want to filter on city AND date, you can do

function(doc) {
   doc.type == 'offer' && emit ([doc.city,doc.expires],doc);
}

and then you can get all offers for a certain city by querying with startkey ["Moscow",Date()]

Functions are untested and I'm a little rusty in Javascript so they may be wrong but you get
the idea :)

Wout.

On Sep 2, 2010, at 12:06 , Mikhail A. Pokidko wrote:

> Hello all!
> 
> I have docs with such fields : {'type':'offer', 'city':''Moscow',
> 'expires':'2010/08/30 22:54:00',}
> And now i`m stuck finding the way how to get unexpired offers for certain city.
> 
> My first attempt was writing view like this:
> function(doc) {
>    var today = new Date();
>    if (doc.type == 'offer') {
>        var created = new Date(doc.expiration_full);
>        if (created > today) {
>                emit(doc.city, doc);
>        }
>    }
> }
> 
> But "today" stays as defined at the moment of view save. (Yes, i
> understand that other way indexes should be recalculated every time)
> 
> So is there any clever way to get unexpired offers for certain city
> without writing view-per-city?startkey=%{today_date} ?
> 
> -- 
> xmpp: pma AT altlinux DOT org


Mime
View raw message