0
votes

I have document which I need to update but updated parameters are not same all the time it depends on user. So how can I update document or replace whole document with new values based on id.

1

1 Answers

0
votes

You wrap your updated values in a Map the set the update:

Map<String, Object> updateInfo; // Key is db column name, value is updatedValue

Then create update operations:

Query<Entity> filter = datastore.createQuery(Entity.class)
                                .field("_id", id);

UpdateOperations<Entity> updateOps = datastore.createUpdateOperations(Entity.class);

updateInfo.entrySet().forEach(e -> updateOps.set(e.getKey(), e.getValue());

datastore.update(filter, updateOps);

By this way, you can update entity with any number of fields