title: Adding to a map in DynamoDB (using Node.js) without knowing ID
body: <p><a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.AddingListsAndMaps" rel="nofollow noreferrer">Reading through the docs</a>, AWS seem to imagine that you somehow know the index numbering of a map that you’re updating. Which is odd, because they don’t even use the numbers in their interface.</p>
  
  <p>I have this object:</p>
  
  <pre><code>let zoo = {
    camels: 562
  }
  </code></pre>
  
  <p>And I’d like to add it to my item:</p>
  
  <pre><code>{
    Key: { id: id },
    TableName: tableName,
    UpdateExpression:
      'SET #z = :z',
    ExpressionAttributeNames: {
      '#z': 'zoo'
    },
    ExpressionAttributeValues: {
      ':z': zoo
    }
  }
  </code></pre>
  
  <p>All good so far. Now I’ve been given a new item to append to the list. It could be more than one:</p>
  
  <pre><code>let zoo = {
    elephants: 126,
    tigers: 89
  }
  </code></pre>
  
  <p>And then my goal is to get my <code>zoo</code> map to look like this:</p>
  
  <pre><code>{
    camels: 562,
    elephants: 126,
    tigers: 89
  }
  </code></pre>
  
  <p>However, I cannot figure out how to append to a map without an ID.</p>
  
  <p>I saw somebody using this:</p>
  
  <pre><code>'SET #z = #z + :z'
  </code></pre>
  
  <p>But that didn’t work at all (<code>Incorrect operand type for operator or function; operator or function: +, operand type: M</code>).</p>
  
  <p>AWS’s docs would suggest that I could do something like this:</p>
  
  <pre><code>'SET #z.#animal = :animal'
  </code></pre>
  
  <p>And then I could possibly loop through my object and generate a <code>SET</code> string that would work. However, this is just a basic example of what I’m trying to do. I would like to be able to add something like this:</p>
  
  <pre><code>let zoo = {
    aquatics: {
      penguins: 988,
      tropical_fish: 1943
    },
    mammals: {
      elephants: 126,
      tigers: 89 
    }
  }
  </code></pre>
  
  <p>I could of course get the item first, merge the map in JavaScript, and then send it back again – but I’m trying to avoid having to make a read/write request.</p>