Hoser
Hoser

Reputation: 5044

Getting alphabetical ordering from a pouchDB query

This is what my code currently looks like:

var physicianList = [];

function map(doc){
    if(doc.PhysicianKey && doc.PhysicianName){
        console.log(doc.PhysicianName);
        emit(doc.PhysicianKey, doc.PhysicianName);
    }
}

dbPhysicianList.query({map: map}, function(error, response){ 
    if(!error){
        angular.forEach(response.rows, function(physician){
            physicianList.push(physician);
        })
        deferred.resolve(physicianList);
    }else{
        console.log(error);
        deferred.reject();
    }}, {include_docs: true});


    return deferred.promise;
}

As you can see, in my map function I'm logging out doc.PhysicianName, and when I do this, they are being logged out in alphabetical order. However, they are getting pushed into the physicianList array in non-alphabetical order, I think by ID order. I would like the final output to be alphabetically ordered in that physicianList array.

For what it's worth, I'm using PouchDB, but I believe the querying is supposed to work exactly like CouchDB.

Upvotes: 3

Views: 909

Answers (1)

Hoser
Hoser

Reputation: 5044

I didn't realize the order of variables of your emit() was the ordering of the result set (I'm a major newbie).

I ended up doing this:

function map(doc){
    emit({physicianName: doc.PhysicianName, physicianKey: doc.PhysicianKey} );
}

And it worked exactly as desired (ordered alphabetically by doc.physicianName).

Upvotes: 2

Related Questions