couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Borja Martín <bor...@dagi3d.net>
Subject Re: sorting documents using multiple keys
Date Thu, 24 Dec 2009 10:13:06 GMT
Hi,
thanks for your answer. The thing is that this solution only works for
single keys and I can't use the startkey/endkey params in the POST request
with the 'keys' param in order to query for multi keys

(Thanks for the include_docs tip :))

Regards

On Thu, Dec 24, 2009 at 3:43 AM, Anand Chitipothu <anandology@gmail.com>wrote:

> > Then, I create a view to store the documents for each tag:
> >
> > function(doc) {
> >    for(var idx in doc.tags) {
> >      emit(doc.tags[idx], doc);
> >    }
> >  }
> >
> > and eventually I can make a POST request using the keys parameter in
> order
> > the retrieve all the documents that have one or more of the given tags.
> > The problem is that I don't know how to sort the result by the field
> > created_at and I can't use it in the emit function within the key as I
> will
> > not know the value for it (and the documents can different values for it)
> >
> > Any idea how to achieve this?
>
> Change your view function to emit both tag and created_at.
>
> function (doc) {
>    for (var i in doc.tags) {
>        emit([doc.tags[i], doc.created_at], null);
>    }
> }
>
> Also notice that you don't have to emit the doc. It can be included in
> the result by adding "include_docs=true" to your query string.
>
> Use the following request to get all documents, matching a tag.
>
> http://localhost:5984/
> <dbname>/_design/<design-doc-name>/<viewname>?start_key=["mytag"]&end_key=["mytag",
> {}]&include_docs=true
>
> If you want the most recent doc first, try adding descending=true and
> swap start_key and end_key.
>
> I'm not sure if there is any straight-forward way to query for more
> than one tag. You can probably achieve that by making your view
> function emit all possible combinations of tags in the document.
>
> Anand
>



-- 
def dagi3d(me)
 case me
   when :web then  "http://dagi3d.net"
   when :twitter then "http://twitter.com/dagi3d"
 end
end

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message