0
votes

I am using Datastore in Firestore mode for my Google App Engine app. I know how to store list/array property values in the Google Cloud Datastore. But how do I update these values (ex: add new values to the list?) I could not find an example in the documentation.

This is how you would add a list property to the datastore initially:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity user = new Entity("User");
List<String> items = new ArrayList<String>();
user.setProperty("ItemsList", items);
datastore.put(user);

But what do I do if later on, I want to access an User entity's list of items and add an item to that list?

1
You can use arrayUnion() and arrayRemove(). Please let me know if this document helpsChris32
That documentation is from Cloud Firestore. I am using Firestore in Datastore mode (aka, I think I only have access to the Datastore methods). Will it still work?HY.

1 Answers

1
votes

Thanks for the clarification. Now I understand that you want to be able to just add things to that list instead of overwriting the whole list.

Reading at the documentation for Datastore I can see that you can't just update a property.

To update an existing entity, modify the properties of the entity previously retrieved and store it using the key

I your case you would do something like retrieve the data of the list, then append the new element or update something in that list and then update the whole list again like:

Entity task = Entity.newBuilder(datastore.get(user)).set("ItemsList", items).build();
datastore.update(user);