Thierry Templier
Thierry Templier

Reputation: 202316

Store and query a list of string in gae datastore with Java

I try to store in the gae datastore an array of string within an Entity. Something like that:

public class MyClass {
  private String id;
  private String name;
  (...)
  private List<String> tags;
  (...)
}

Entity entity = new Entity("MyClass", "myId");
entity.setProperty("name", "My Name");
List<String> tags = new ArrayList<String>();
tags.add("gae");
tags.add("datastore");
entity.setProperty("tags", tags);

Is this approach the correct one and how to use the tags property in queries (for example, all elements that have the value gae in the tags list)?

Query query = new Query("MyClass");
query.addFilter(fieldName, FilterOperator.EQUAL, "gae");
Iterable<Entity> result = datastore.prepare(q).asIterable();
for (Entity entity : result) {
  (...)
}

Thanks for your help! Thierry

Upvotes: 1

Views: 1723

Answers (1)

fmt.Println.MKO
fmt.Println.MKO

Reputation: 2060

yes thats right, if your tags list doesn't get to big, (more then 10k, have a look at the field size limits)

you can query now you MyClass like this:

Query q = new Query("myClass")
                .addFilter("tags",
                           Query.FilterOperator.EQUAL,
                           "gae");

Upvotes: 2

Related Questions