gpt4 book ai didi

couchdb - 如何在不提供 "limit"字段的情况下使用 Mango 查询 couchDB 获取无限文档?

转载 作者:行者123 更新时间:2023-12-01 04:47:18 24 4
gpt4 key购买 nike

我在沙发数据库中使用 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

})

关于couchdb - 如何在不提供 "limit"字段的情况下使用 Mango 查询 couchDB 获取无限文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45632794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com