PouchDB 查询图 - 找到最高值

PouchDB query map - Find the highest value

如何找到字段值最高的文档x

map = function(doc) {
  // here, how do I emit the document with the highest value of 'doc.x'?
}

db.query(map, {include_docs: true}).then(function(doc) {
    console.log(doc.x)
})

这可能不是最好的方法,但我是这样做的:

db.allDocs({include_docs: true}).then(function(docs) {

   var highest;

   $.each(docs.rows, function() {
     var item = $(this)[0].doc

     if (highest == undefined || item.x > highest) {
       highest = item.x
     }
   })

   map = function(doc, emit) {
     if (doc.x == highest) {
       emit([doc.x])
     }
   }

   db.query(map, {include_docs: true}).then(function(result) {
     console.log(result)
   })
})