Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

sort the queryView results by different attribute #100

Open
gayancharith opened this issue Dec 4, 2016 · 5 comments
Open

sort the queryView results by different attribute #100

gayancharith opened this issue Dec 4, 2016 · 5 comments
Labels

Comments

@gayancharith
Copy link

gayancharith commented Dec 4, 2016

When using the queryView method, I want filter the documents by one attribute and sort by another attribute. Currently I can sort the result only by emitting attributes.

@jamesnocentini
Copy link
Contributor

Can you give an example of what the filter and sort attributes might be?

@gayancharith
Copy link
Author

gayancharith commented Dec 7, 2016

    person_name_view: {
      "map": function (doc) {
        if(doc.type === 'person') {
          emit(doc.firstname.toLowerCase(), null);
        }
      }.toString()
    },

here I want to filter persons from first name but I want sort them by their age.

@npomfret
Copy link
Contributor

npomfret commented Dec 8, 2016

I think what you need to do is add the age to the index. Something like:

person_name_view: {
      "map": function (doc) {
        if(doc.type === 'person') {
          emit([doc.firstname.toLowerCase(), doc.age], null);
        }
      }.toString()
    },

They will be ordered naturally, so you can use the name in your key parameter and I understand they will come out in ascending age order.

@gayancharith
Copy link
Author

@npomfret it is working perfeclty. But I am kind of confused since if I want to emit more keys (including more integer attributes) but I want sort from only one specific key, how should I do it ?

@gayancharith
Copy link
Author

@jamiltz I think it is nice to have an option to tell whether which attribute I need to sort by.

@djpongh djpongh added the icebox label Apr 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants