6

我在沙发数据库中使用 Mango 查询来查找文档的几乎每个查询。在这里,我在获取与给定条件匹配的所有文档时遇到了问题。问题是芒果查询的默认限制是 25(意味着每个查询获取 25 个文档),并且我的数据库中有很多文档,我没有确切的文档数。我不能硬编码芒​​果查询中的限制,因为我不知道文档的上限,而且我认为硬编码限制不是一个好主意。谁能帮我解决这个问题?我怎样才能将限制设为无限制,或者有没有其他方法可以处理这种情况?

4

2 回答 2

2

我反驳了同样的问题并用递归函数解决了它

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

})
于 2017-10-30T18:12:33.723 回答
0

显然bookmark,每个 _find 调用都会返回一个键,然后您可以将其传递回另一个 _find 调用以获取结果的下一个“页面”(假设第二个查询与第一个查询相比没有任何其他更改)。

如果不能保证之前的查询相同,则不确定这种方法有多实用。

我实际上最终使用了@Erik 答案的迭代版本。

在此处查看文档中的 JSON 请求选项:https ://docs.couchdb.org/en/latest/api/database/find.html

于 2021-07-20T23:45:03.170 回答