title: AWS Lambda: How to Add Numbers to a NS Set in Dynamodb
body: <h2>The Issue</h2>
  
  <p>I have tried several approaches, but haven't been able to find out how to add numbers to a NS set. This is all running inside a lambda function.</p>
  
  <h2>What I'm trying to accomplish</h2>
  
  <p>I am creating a dynamodb table where different colors in hex align to a set of ids. I am optimizing the table for fast reads and to avoid duplicates which is why I would like to maintain a set of ids for each hex.</p>
  
  <p>How I'm adding items to the table:</p>
  
  <pre><code>let doc = require('dynamodb-doc');
  let dynamo = new doc.DynamoDB();
  
  var object = {
      'TableName': 'Hex',
      'Item': {
          'hex': '#FEFEFE',
          'ids': {
              'NS': [2,3,4]
          }
      }
  }
  
  dynamo.putItem(object, callback);
  </code></pre>
  
  <h2>Which results in</h2>
  
  <p><a href="https://i.stack.imgur.com/67jAR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/67jAR.png" alt="results after adding to table"></a></p>
  
  <h2>Then I try to add more ids to the set</h2>
  
  <p>Using the <a href="http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html" rel="noreferrer">Dynamodb Update Item Documentation</a> standards</p>
  
  <pre><code>var params = {
      "TableName" : "Hex",
      "Key": {
        "hex": "#FEFEFE"
      },
      "UpdateExpression" : "ADD #oldIds :newIds",
      "ExpressionAttributeNames" : {
        "#oldIds" : "ids"
      },
      "ExpressionAttributeValues": { 
        ":newIds" : {"NS": ["5", "6"]}
     },
  };
  
  dynamo.updateItem(params, callback);
  </code></pre>
  
  <p>This returns the following error, so dynamo thinks :newIds is a map type instead of a set(?)</p>
  
  <pre><code>"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"
  </code></pre>
  
  <h2>I have also tried these alternative approaches</h2>
  
  <p><strong>Try 2:</strong></p>
  
  <pre><code>var setTest = new Set([5, 6]);
  
  var params = {
      "TableName" : "Hex",
      "Key": {
        "hex": "#FEFEFE"
      },
      "UpdateExpression" : "ADD #oldIds :newIds",
      "ExpressionAttributeNames" : {
        "#oldIds" : "ids"
      },
      "ExpressionAttributeValues": { 
        ":newIds" : setTest
     },
  };
  
  dynamo.updateItem(params, callback);
  </code></pre>
  
  <p>Error 2 (same error):</p>
  
  <pre><code>"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"
  </code></pre>
  
  <p><strong>Try 3:</strong> </p>
  
  <pre><code>var params = {
      "TableName" : "Hex",
      "Key": {
        "hex": "#FEFEFE"
      },
      "UpdateExpression" : "ADD #oldIds :newIds",
      "ExpressionAttributeNames" : {
        "#oldIds" : "ids"
      },
      "ExpressionAttributeValues": { 
        ":newIds" : { "NS" : { "L" : [ { "N" : "5" }, { "N" : "6" } ] }}
     },
  };
  
  dynamo.updateItem(params, callback);
  </code></pre>
  
  <p>Error 3 (same error):</p>
  
  <pre><code>"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"
  </code></pre>
  
  <p><strong>Try 4:</strong></p>
  
  <pre><code>var params = {
      "TableName" : "Hex",
      "Key": {
        "hex": "#FEFEFE"
      },
      "UpdateExpression" : "ADD #oldIds :newIds",
      "ExpressionAttributeNames" : {
        "#oldIds" : "ids"
      },
      "ExpressionAttributeValues": { 
        ":newIds" : [5,6]
     },
  };
  
  dynamo.updateItem(params, callback);
  </code></pre>
  
  <p>Error 4 (similar error, but dynamo thinks I'm adding a list this time)</p>
  
  <pre><code>"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST"
  </code></pre>
  
  <h2>Stack Overflow/Github Questions I've Tried</h2>
  
  <p><a href="https://stackoverflow.com/a/37585600/4975772">https://stackoverflow.com/a/37585600/4975772</a> (I'm adding to a set, not a list)</p>
  
  <p><a href="https://stackoverflow.com/a/37143879/4975772">https://stackoverflow.com/a/37143879/4975772</a> (I'm using javascript, not python, but I basically need this same thing just different syntax)</p>
  
  <p><a href="https://github.com/awslabs/dynamodb-document-js-sdk/issues/40#issuecomment-123003444" rel="noreferrer">https://github.com/awslabs/dynamodb-document-js-sdk/issues/40#issuecomment-123003444</a> (I need to do this exact thing, but I'm not using the dynamodb-document-js-sdk, I'm using AWS Lambda</p>