Repository navigation
Datastore/dataset question #690
Description
Activity
I think you might do this with a keys-only query. @stephenplusplus , how do we do that in gcloud-node?
- addedtype: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.
on Jun 25, 2015 looking at GQL I think it would be the following:
SELECT __key__ FROM myKindgoing to look at dataset class right now.but what if the keys being returned is a ridiculous amount?
You can run a keys-only query by using
select('__key__')when building your query. Here's a start to finish example:var dataset = gcloud.datastore.dataset(); var personKey = dataset.key(['Person', 'Stephen']); dataset.save({ key: personKey, data: { alias: 'stephenplusplus' } }, function(err) { var normalQuery = dataset.createQuery('Person'); var keysOnlyQuery = normalQuery.select('__key__'); dataset.runQuery(keysOnlyQuery, function(err, results) { // results = [ // { // key: { // path: ['Person', 'Stephen'] // }, // // data: {} // } // ] }); dataset.runQuery(normalQuery, function(err, results) { // results = [ // { // key: { // path: ['Person', 'Stephen'] // }, // // data: { // alias: 'stephenplusplus' // } // } // ] }); });
will the following construct work:
var query = dataset.createQuery('Person').filter('age >', '20').select('__key__'); dataset.runQuery(query, function(err, results) { // results = [ // { // key: { // path: ['Person', 'Stephen'] // }, // // data: {} // }, // { // key: { // path: ['Person', 'Alice'] // }, // // data: {} // }, // { // key: { // path: ['Person', 'Bob'] // }, // // data: {} // } // ] });will test later on, and come back if I have any questions. Thanks for the help guys @stephenplusplus @jgeewax
This will still return all the keys, which is O(n) in the payload size, when you really just want one integer, which will be O(1).
Is there a way to ask for COUNT(key)?
@dhermes Exactly, that is the ideal solution, but not sure if COUNT func is supported by GQL.
I couldn't find anything in the docs about just getting the resulting length of a query. @pcostell might know if this is possible?
Nope, Datastore has no way to count. Even in the SDKs which offer a
countmethod, these are all O(n).
Here be dragons and all that:
I would not recommend implementing this, but the "fastest" way to do this would be to run a your query with
projection = '__key__'withoffset = 1000andlimit = 0. Then keep running this counting the number ofskipped_resultsand using theskipped_cursorto get the next batch.This way, Datastore doesn't return any results, just the counts. However, you still have to pay for each batch (the key projection makes it only one read per batch though). More importantly, Datastore still has to load all the results, so the time it takes (and the cost) will scale with the size of your count.
Yea, I think mentioning that somewhere (ever so briefly) would be a good idea...
6 remaining items
- added a commit that references this issue
on Jul 23, 2025 - added 7 commits that reference this issue
on Jan 27, 2026 - added a commit that references this issue
on Mar 12, 2026 - added a commit that references this issue
on Mar 27, 2026
is there a way to get the count/length of the entities that matched a query without actually receiving the entities data? or can something like that be achieved via BigQuery?