How to programmatically insert and update Field Collection values?
The following snippet (compatible with field_collection 8.x-1.0-alpha1) reuses an existing FieldCollectionItem or creates a new one on demand. The last few lines show how to modify fields inside the FieldCollectionItem.

If one has a field collection with a different name, then it should be enough to replace all occurrences of field_test_field_collection with the custom field collection's machine name.

&lt;?php

// […]

$nodeId = 123;

/** @var EntityStorageInterface $entityStorage */
$entityStorage = Drupal::entityTypeManager()-&gt;getStorage('node');

$node = $entityStorage-&gt;load($nodeId);
$node-&gt;body = 'Body Update: ' . date('c');

/** @var Drupal\Core\Field\FieldItemList $fieldItemList */
$fieldItemList = $node-&gt;field_test_field_collection;

// Reuse existing Field Collection.
if (count($fieldItemList))
{
    /** @var \Drupal\field_collection\Plugin\Field\FieldType\FieldCollection $fieldCollection */
    $fieldCollection = $fieldItemList[0];
    $fieldCollectionItem = FieldCollectionItem::load($fieldCollection-&gt;value);
}

// Create new Field Collection.
else
{
    $fieldCollectionItem = FieldCollectionItem::create([
        'field_name' =&gt; 'field_test_field_collection',
    ]);

    $fieldCollectionItem-&gt;setHostEntity($node);

    $node-&gt;field_test_field_collection[] = [
        'field_collection_item' =&gt; $fieldCollectionItem,
    ];
}

$fieldCollectionItem-&gt;set('field_text', 'Last Update: ' . date('c'));

$node-&gt;save();


Deleting an existing collection is as simple as: fieldCollectionItem-&gt;delete();

Please have a look at the source code of the unit test class Drupal\field_collection\Tests\FieldCollectionBasicTestCase for more information.