Reputation: 73
My Kind has 3 entities: FirstName, FamilyName and Email.I want to retrieve only the Key and the FirstName associated with the entity. Like this in SQL : SELECT Id,FirstName from users;
In go-lang, I tried fetching all the data in the Kind like this
q := datastore.NewQuery(dataKind)
and then to get the keys, I do this:
keys, err := q.GetAll(ctx, &users)
I don't want to fetch all the properties, instead only the keys and the FirstName. I was wondering if there is a way to do it in a single datastore query? As mentioned earlier in my previous question, I'm new to go-lang and datastore. Please help
Upvotes: 1
Views: 602
Reputation: 121169
Use Project to select a single property. The property must be indexed. The query does not return entities where the property is not set.
The following snippet returns the keys and users with only the FristName field set:
q := datastore.NewQuery(dataKind).Project("FirstName")
keys, err := client.GetAll(ctx, q, &users)
Upvotes: 1