sap
sap

Reputation: 1141

Google DataStore - how to fetch data by ID/Name?

I'm trying to write a query for DataStore.

I know that DataStore automatically assigns a unique "ID/Name" to each row and I wanted to fetch data from a row with a specific "ID/Name".

enter image description here

I'm doing the following but it's not working:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("ListOfMessages");
query.addFilter("**ID/Name**", Query.FilterOperator.EQUAL, message_id);
List<Entity> users = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1));
for (Entity user : users) 
{
    message=(String) user.getProperty("smil_message");
}
System.out.println("message"+message);

What am I doing wrong?

Upvotes: 2

Views: 4021

Answers (1)

Riley Lark
Riley Lark

Reputation: 20890

The best way is with get instead of querying:

datastoreService.get(KeyFactory.createKey("ListOfMessages", message_id));

If you must query, the property name is __key__ and you need to filter on a fully-constructed key, not just the message_id.

query.addFilter("__key__", Query.FilterOperator.EQUAL, KeyFactory.createKey("ListOfMessages", message_id));

Upvotes: 2

Related Questions