title: Transactional Update and Removal of documents in ArangoDB
body: <p>Given the following document structure:</p>
  
  <pre><code>{
      "id": "123"
      "traits":
       {
          "abc": 6.5
          "def: 66
       }
  }
  </code></pre>
  
  <p>I need to iterate over the documents and remove some of the traits based on criteria. 
  A document with all traits removed should be removed as well.</p>
  
  <p>Finally, I need to keep track of how many traits and documents were removed.</p>
  
  <p>The update and removal operations should not be blocking and may be executed while these documents are being updated or queried.</p>
  
  <p>I have implemented this in python using python-arango by using an update/replace query to remove traits and a remove query to remove documents without traits by executing the following queries:</p>
  
  <pre><code>FOR some_doc IN some_collection
      FILTER &lt;some filter&gt;
      LET updated_doc = ...
      REPLACE some_doc with updated_doc in some_collection OPTIONS { ignoreRevs: false, ignoreErrors: true }
  </code></pre>
  
  <pre><code>FOR some_doc IN some_collection
      FILTER LENGTH(some_doc.traits)==0
      REMOVE some_doc in some_collection OPTIONS { ignoreRevs: false, ignoreErrors: true }
  </code></pre>
  
  <p>I then pull statistics from each returned cursor:</p>
  
  <pre><code>cursor = db.aql.execute(remove_traits_query)
  stats = cursor.statistics()
  modified = stats['modified']
  </code></pre>
  
  <p>The problem is I need to prevent the possibility that a lookup query initiated during the execution of the above process returns a document with an empty traits object, before the 2nd query (i.e. remove query) is complete.</p>
  
  <p>I tried implementing a transaction then pulling job cursor stats post commit like this:</p>
  
  <pre><code>trx_db = db.begin_transaction(write=collection)
  traits_removal_job = trx_db.aql.execute(remove_traits_query)
  doc_deletion_job = trx_db.aql.execute(delete_query)
  trx_db.commit()
  stats = traits_removal_job.result().statistics()
  </code></pre>
  
  <p>but the cursors of the transaction jobs are empty.
  I suppose that's because ArangoDB executes transaction as a single Javascript function.</p>
  
  <p>I could filter out empty traits on all lookup queries, but it would be better if I could execute the above removal/update operations either in a single query (impossible in ArangoDB per <a href="https://www.arangodb.com/docs/stable/aql/operations-update.html" rel="nofollow noreferrer">documentation</a>), or in a transaction (no execution stats?).</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks in advance!</p>