incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anand Chitipothu <anandol...@gmail.com>
Subject Re: sorting documents using multiple keys
Date Thu, 24 Dec 2009 02:43:09 GMT
> 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

Mime
View raw message