Aron
Aron

Reputation: 1179

Google Cloud Datastore query values in array

I have entities that look like that:

{
  name: "Max",
  nicknames: [
   "bestuser"
 ]
}

how can I query for nicknames to get the name?

I have created the following index,

indexes:

- kind: users
  properties:
  - name: name
  - name: nicknames

I use the node.js client library to query the nickname,

db.createQuery('default','users').filter('nicknames', '=', 'bestuser')

the response is only an empty array.

Is there a way to do that?

Upvotes: 1

Views: 3588

Answers (4)

Yurci
Yurci

Reputation: 576

If you are using the default namespace you need to remove it from your query. Your query needs to be like this:

const query = db.createQuery('users').filter('nicknames', '=', 'bestuser')

Upvotes: 0

JLCDev
JLCDev

Reputation: 629

The first point to notice is that you don't need to create an index to this kind of search. No inequalities, no orders and no projections, so it is unnecessary.

As Reuben mentioned, you've created the query but you didn't run it.

ds.runQuery(query, (err, entities, info) => {
                if (err) {
                    reject(err);
                } else {
                    response.resultStatus = info.moreResults;
                    response.cursor = info.moreResults == TNoMoreResults? null: info.endCursor;
                    resolve(entities);
                };
            });

In my case, the response structure was made to collect information on the cursor (if there is more data than I've queried because I've limited the query size using limit) but you don't need to anything more than the resolve(entities)

Upvotes: 0

netskink
netskink

Reputation: 4539

I read the entire plob as a string to get the bytes of a binary file here. I imagine you simply parse the Json per your requirement

Upvotes: -1

Reuben van Ammers
Reuben van Ammers

Reputation: 21

You need to actually fetch the query from datastore, not just create the query. I'm not familiar with the nodejs library, but this is the code given on the Google Cloud website:

datastore.runQuery(query).then(results => {
  // Task entities found.
  const tasks = results[0];

  console.log('Tasks:');
  tasks.forEach(task => console.log(task));
});

where query would be

const query = db.createQuery('default','users').filter('nicknames', '=', 'bestuser')

Check the documentation at https://cloud.google.com/datastore/docs/concepts/queries#datastore-datastore-run-query-nodejs

Upvotes: 2

Related Questions