daiyue
daiyue

Reputation: 7448

How to delete a property/value from an entity using google cloud datastore

I am learning google.cloud.datastore, and like to know how to delete a property along with its value from an entity. Also, is it possible to delete a specific or a list of properties from all entities of a certain kind?

My understanding is datastore stores/manipulates data in a row-wise way (entities)?

cheers

Upvotes: 2

Views: 2387

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39814

Your understanding is correct, all datastore write operations happen, indeed, at the entity level. So in order to modify one or a subset of properties you'd retrieve the entity, modify the property (or delete it, if you want to delete the property) set and save the entity.

The exact details depend on the language and library used. From Updating an entity:

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

PYTHON

with client.transaction():
    key = client.key('Task', 'sample_task')
    task = client.get(key)

    task['done'] = True

    client.put(task)

The object data overwrites the existing entity. The entire object is sent to Cloud Datastore. If the entity does not exist, the update will fail. If you want to update-or-create an entity, use upsert as described previously.

Note: To delete a property, remove the property from the entity, then save the entity.

In the above snippet, for example, deleting the done property of the task entity, if existing, would be done like this:

with client.transaction():
    key = client.key('Task', 'sample_task')
    task = client.get(key)

    if 'done' in task:
        del task['done']
        client.put(task)

Upvotes: 4

Related Questions