2017-08-11 29 views
1

我在沙发数据库中使用芒果查询几乎每个查询来查找文档。在这里,我在获取符合给定条件的所有文档时遇到了一个问题。问题是芒果查询的默认限制是25(意味着每个查询获取25个文档)并且我的数据库中有很多文档,并且我没有确切的文档数量。我不能硬编码芒​​果查询的限制,因为我不知道文档的上限,我不认为编码极限是一个好主意。 任何人都可以请帮我解决这个问题吗?我该如何将限制限制为无限制或者是否有其他方法来处理这种情况?如何在不提供“限制”字段的情况下使用芒果查询couchDB获取无限制文档?

+0

使用CouchDB 2.1解决它,你可以更改默认限制。因此,我认为你不能避免这个限制。无论如何,你应该有一个限制,因为你有更多的结果,处理和发送通过http需要的时间越长。你应该总是设计你的代码来处理请求的分页。 –

+0

@AlexisCôté谢谢亚历克西斯! :)我会采用分页方法,因为它似乎是一个更好的选择,在这一点上唯一的选择。 –

+0

我的不好,分页还没有被支持(看起来它会成为下一个版本的一部分),我想你可以暂时把一个高数字作为极限。 –

回答

1

我反驳了同样的问题,用递归函数

const batchSize = 25; 
let batchCount = 0; 
let searchResults = []; 

// in my case I want to find docs with a certain date, hardcoded here as example 
let selectedDate = '2017/10/30'; 

// the recursive function 
let search = function (count, limit){ 
    return db.find({ 
     selector: { 
      date: selectedDate 
     }, 
     limit: batchSize, 
     skip: batchCount*batchSize 
    }).then(function(batch){ 
     if (batch.docs.length === 0){ 
      // there are no (more) search results, so we can return what we have 

      return searchResults 

     } else { 
      // there may be more search results, so we add this batch to the result and call the function again to ask more. We increase the counter so that the first batch(es) are skipped 

      for (var i = 0; i < batch.docs.length; i++) { 
       searchResults.push(batch.docs[i]) 
      } 
      batchCount ++ 
      return search(batchCount, batchSize) 
     } 
    }) 
} 

search(batchCount, batchSize).then(function(result){ 
    // you can handle the searchresults 

}) 
相关问题