Update all nodes with a default field after adding a new field to the content type I have an existing content type with a number of nodes. I have just added a new boolean field with a default value of "off." 

However, until I re-save each node the default value is not set, so a view that should shown only those nodes where the new field is using the default value is currently empty.

How can I update the existing nodes, created before the field has been added, to set that field to its default value?
Sadly, there's no very simple way to do this (besides VBO/rules), but here's the code I use in update functions in my custom module install files when I need to prepopulate field values for a certain node type after adding a new field (in this case, 'page' nodes):

&lt;?php
// Get all nodes of 'page' type, populate the field with a value of '1'.
$pages = db_query("SELECT nid, vid FROM {node} WHERE type = 'page'")-&gt;fetchAllAssoc('nid');
foreach ($pages as $nid =&gt; $values) {
  $fields = array(
    'entity_type' =&gt; 'node',
    'bundle' =&gt; 'page',
    'deleted' =&gt; 0,
    'entity_id' =&gt; $values-&gt;nid,
    'revision_id' =&gt; $values-&gt;vid,
    'language' =&gt; LANGUAGE_NONE,
    'delta' =&gt; 0,
    'field_page_new_field_value' =&gt; 1,
  );
  db_insert('field_data_field_page_new_field')
    -&gt;fields($fields)
    -&gt;execute();
  db_insert('field_revision_field_page_new_field')
    -&gt;fields($fields)
    -&gt;execute();
}
?&gt;


Another manual method mentioned in this answer uses EntityFieldQuery and loads/saves each node. More Drupal-y, but way less performant... (requires a full node load and save operation for each node!).