How can I update the field of the entity? I have an "Question" entity, and I need to updates its 'key' field (of string type): 

My code isn't working:

  $storage = \Drupal::entityManager()-&gt;getStorage('question');
  $query = $storage-&gt;getQuery();
  $query
          -&gt;condition('key', $key);

  $entity = $query-&gt;execute();


 $entity -&gt; set ('key', 'new_value');
 $entity-&gt;save();


It returns the error Fatal error: Call to a member function set() on a non-object
The QueryInterface::execute() method returns either an integer or an array, not an object. It is a good idea to check api.drupal.org (if you don't use an IDE), or let your IDE do the work for you with intellisense.

It probably would be easier to use loadByProperties, which loads the entities for you. However it is definitely useful to get the query object, but you need to call loadMultiple.

$entities = \Drupal::entityTypeManager()
  -&gt;getStorage('question')
  -&gt;loadByProperties(['key' =&gt; $key]);

if (!empty($entities)) {
  $entity = reset($entities);
  $entity-&gt;set('key', 'new_value');
  $entity-&gt;save();
}