从pouchDB查询中获取字母顺序

时间:2014-11-14 17:42:13

标签: couchdb pouchdb

这就是我的代码目前的样子:

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;
}

正如您所看到的,在我的地图功能中,我正在注销doc.PhysicianName,当我这样做时,它们将按字母顺序注销。但是,按照ID顺序,它们会以非字母顺序被推入physicianList数组。我希望最终输出在该physicianList数组中按字母顺序排列。

对于它的价值,我正在使用PouchDB,但我相信查询应该像CouchDB一样工作。

1 个答案:

答案 0 :(得分:2)

我没有意识到你emit()的变量顺序是结果集的排序(我是一个主要的新手)。

我最终这样做了:

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

它完全符合要求(按doc.physicianName按字母顺序排序)。