Google云数据存储,如何查询更多结果

简单而言,我有以下function,使用Google Cloud Datastore Node.js API:

fetchAll(query, result=[], queryCursor=null) { this.debug(`datastoreService.fetchAll, queryCursor=${queryCursor}`); if (queryCursor !== null) { query.start(queryCursor); } return this.datastore.runQuery(query) .then( (results) => { result=result.concat(results[0]); if (results[1].moreResults === _datastore.NO_MORE_RESULTS) { return result; } else { this.debug(`results[1] = `, results[1]); this.debug(`fetch next with queryCursor=${results[1].endCursor}`); return this.fetchAll(query, result, results[1].endCursor); } }); } 

Datastore API对象位于variablesthis.datastore ;

该函数的目标是获取给定查询的所有结果,尽pipe每个runQuery调用返回的项目数量有限制。

我还没有发现Datastore API对此有任何确定的硬性限制,而且这个文档似乎有点不透明,但是我只注意到我总是得到results[1] = { moreResults: 'MORE_RESULTS_AFTER_LIMIT' } ,这表明还有更多的结果被提取,并且results[1].endCursor仍然停留在每次迭代再次传递的常量值上。

所以,给一些简单的查询,我插入这个函数,我只是继续运行查询,设置查询开始游标(通过执行query.start(queryCursor); )到前一个查询结果中获得的endCursor 。 显然,我的希望是在本次迭代中的每个连续查询中获得下一组结果。 但是我总是得到相同的results[1].endCursorresults[1].endCursor 。 我的问题是: 为什么?

从概念上来说,我看不出在Google文档中给出的这个示例有什么不同:

 // By default, google-cloud-node will automatically paginate through all of // the results that match a query. However, this sample implements manual // pagination using limits and cursor tokens. function runPageQuery (pageCursor) { let query = datastore.createQuery('Task') .limit(pageSize); if (pageCursor) { query = query.start(pageCursor); } return datastore.runQuery(query) .then((results) => { const entities = results[0]; const info = results[1]; if (info.moreResults !== Datastore.NO_MORE_RESULTS) { // If there are more results to retrieve, the end cursor is // automatically set on `info`. To get this value directly, access // the `endCursor` property. return runPageQuery(info.endCursor) .then((results) => { // Concatenate entities results[0] = entities.concat(results[0]); return results; }); } return [entities, info]; }); } 

(除了我自己并没有指定查询结果大小的限制,我也尝试过,将其设置为1000,这不会改变任何内容)。

为什么我的代码运行到这个无限循环,停留在同一个“endCursor”的每一步? 我该如何纠正?

另外,每次调用datastore.runQuery()获得的结果的数量是多less? 到目前为止,我还没有在Google Datastore文档中find这些信息。

谢谢。

查看Datastore的Node.js客户端库的API文档 ,该页面上有一个标题为“分页logging”的部分,可以帮助您。 以下是该部分代码片段的直接副本:

 var express = require('express'); var app = express(); var NUM_RESULTS_PER_PAGE = 15; app.get('/contacts', function(req, res) { var query = datastore.createQuery('Contacts') .limit(NUM_RESULTS_PER_PAGE); if (req.query.nextPageCursor) { query.start(req.query.nextPageCursor); } datastore.runQuery(query, function(err, entities, info) { if (err) { // Error handling omitted. return; } // Respond to the front end with the contacts and the cursoring token // from the query we just ran. var frontEndResponse = { contacts: entities }; // Check if more results may exist. if (info.moreResults !== datastore.NO_MORE_RESULTS) { frontEndResponse.nextPageCursor = info.endCursor; } res.render('contacts', frontEndResponse); }); }); 

也许你可以尝试使用其他语法选项(而不是Promise)。 runQuery方法可以将callback函数作为参数,并且该callback的参数包括显式引用实体数组和信息对象(将endCursor作为属性)。

而且对数据存储API调用也有限制和配额。 以下是详细解决这些问题的官方文档的链接:

范围

配额