Van*_*ain 6 couchdb couchdb-mango
我在沙发数据库中使用 Mango 查询几乎每个查询来查找文档。在这里,我在获取与给定条件匹配的所有文档时遇到问题。问题是 mango 查询的默认限制是 25(意味着每个查询获取 25 个文档)并且我的数据库中有很多文档,我没有确切的文档数。我无法在 mango 查询中对限制进行硬编码,因为我不知道文档的上限,而且我认为对限制进行硬编码不是一个好主意。任何人都可以帮我解决这个问题吗?我怎样才能将限制设置为无限制,或者有没有其他方法来处理这种情况?
我遇到了同样的问题并用递归函数解决了它
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
})
Run Code Online (Sandbox Code Playgroud)