title: Correcting database structure with MongoDB
body: <p>I have a fairly straight forward database structure I would like to create with MongoDB. My structure looks like this:</p>
  
  <pre><code>    {url: value,
       users: {user_id: value}}
  </code></pre>
  
  <p>An example document might looks like this:</p>
  
  <pre><code>{'url': 'http://stackoverflow.com/', users: {'billy': 12, 'tommy': 2}}
  </code></pre>
  
  <p>I'm using PyMongo to update my database and add documents as show above. I can successfully add data to my database, but I cannot seem to duplicate the above data structure. </p>
  
  <pre><code>    for d in data:
          foo.update(
              {'id': d.get('url')}, 
              {'$addToSet' :{'users': {d.get('user'): 'NaN'}}},
              upsert=True)
  </code></pre>
  
  <p>The above code inserts but the 'user' structure is not what I intended. I would like:</p>
  
  <pre><code>users: {'billy': 'NaN', 'tommy': 'NaN'}
  </code></pre>
  
  <p>and I get:</p>
  
  <pre><code>users : [ { 'billy' : "NaN" }, { 'tommy' : "NaN" } ]
  </code></pre>
  
  <p>Can anyone help? Thanks for your time.</p>