How to perform a "key-only query" in Google Cloud Storage (Node.js)

I'm trying to do a "key-only request" with the Google Datastore API for Node.js as shown in the documentation here .

I do this after saving several entries like this:

datastore.save(
  records.map(
    (record) => {
      return {
        key: datastore.key([kind, record.id]),
        data: record,
      };
    }
  )

      

A constant kind

is a string. Each record

has a valid unique property id

(number), which, as shown here, should also serve as a data store key identifier.

The records are stored correctly. I can get them without problem through

datastore.runQuery(datastore.createQuery(kind))
.then( (results) => {
    // whatever...
}

      

All saved records are returned correctly.

But when I make a "key-only request" (and as shown in the documentation ):

const query = datastore.createQuery(kind)
  .select('__key__');
datastore.runQuery(query)
.then( (results) => {
    // whatever...
}

      

my results[0]

return value is just an array of empty objects like:

results[0]: [ {}, {}, {}, {}, {}, ..., {}]

      

The number of empty objects returned here is the correct number of records of this type. But the problem is that they are empty objects. I was expecting to get the datastore key for every record here.

If, on the other hand, I make a "normal" projection request to a "normal" property (for example, "id" - which should be identical to the datastore key, as far as I understand, after defining the key through datastore.key[kind, record.id]

), I get the projected "id" properties correctly ":

const query = datastore.createQuery(kind)
  .select('id');
datastore.runQuery(query)
.then( (results) => {
    // whatever...
}

      

Result:

results[0]: [ 
  { id: 5289385927 },
  { id: 5483575687 },
  { id: 5540575111 },
  { id: 5540622279 },
  // ... and so on
]

      

So what's wrong with my "key-only-query"? I did it exactly as described in the documentation. But I am only getting empty results.

NOTE: I've only tested this in the datastore emulator. Same result in datastore emulator as in AppEngine.

+3


source to share


1 answer


You can get the key of an entity using the symbol datastore.KEY



var keys = results.map(function(res) {
     return res[datastore.KEY];
});

      

+3


source







All Articles