title: Inserting a document with Pymongo - InvalidDocument: Cannot encode object
body: <p>I am trying to insert a document (twitter information in this case) into Mongo database with PyMongo. </p>
  
  <p>As you can see below, tweets_listdt[0] is exactly the same as </p>
  
  <pre><code>{
       'created_at': u'Sun Aug 03 17:07:24 +0000 2014',
       'id': 2704548373,
       'name': u'NoSQL',
       'text': u'RT @BigdataITJobs: Data Scientist \u2013 Machine learning, Python, Pandas, Statistics @adam_rab in London, United Kingdom http://t.co/pIIJVPCuN8\u2026'
  }
  </code></pre>
  
  <p>But I couldn't save tweets_listdt[0] into my Mongodb while I could do so with the later one.</p>
  
  <pre><code>In[529]: tweets_listdt[0] == {'created_at': u'Sun Aug 03 17:07:24 +0000 2014',
   'id': 2704548373,
   'name': u'NoSQL',
   'text': u'RT @BigdataITJobs: Data Scientist \u2013 Machine learning, Python, Pandas, Statistics @adam_rab in London, United Kingdom http://t.co/pIIJVPCuN8\u2026'}
  Out[528]: **True**
  </code></pre>
  
  <p>This one fails:</p>
  
  <pre><code>In[530]: tweetsdb.save(tweets_listdt[0])
  tweetsdb.save({'created_at': u'Sun Aug 03 17:07:24 +0000 2014',
   'id': 2704548373,
   'name': u'NoSQL',
   'text': u'RT @BigdataITJobs: Data Scientist \u2013 Machine learning, Python, Pandas, Statistics @adam_rab in London, United Kingdom http://t.co/pIIJVPCuN8\u2026'})
  Traceback (most recent call last):
    File "D:\Program Files\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "&lt;ipython-input-529-b1b81c04d5ad&gt;", line 1, in &lt;module&gt;
      tweetsdb.save(tweets_listdt[0])
    File "D:\Program Files\Anaconda\lib\site-packages\pymongo\collection.py", line 1903, in save
      check_keys, manipulate, write_concern)
    File "D:\Program Files\Anaconda\lib\site-packages\pymongo\collection.py", line 430, in _insert
      gen(), check_keys, self.codec_options, sock_info)
  InvalidDocument: **Cannot encode object: 2704548373**
  </code></pre>
  
  <p>This one works okay:</p>
  
  <pre><code>In[531]: tweetsdb.save({'created_at': u'Sun Aug 03 17:07:24 +0000 2014',
   'id': 2704548373,
   'name': u'NoSQL',
   'text': u'RT @BigdataITJobs: Data Scientist \u2013 Machine learning, Python, Pandas, Statistics @adam_rab in London, United Kingdom http://t.co/pIIJVPCuN8\u2026'})
  Out[530]: **ObjectId('554b38d5c3d89c09688b1149')**
  </code></pre>
  
  <h3>Update on 5/10</h3>
  
  <p>Thanks Bernie. The PyMongo version I'm using is 3.0.1.</p>
  
  <p>here is the check of id's data type:</p>
  
  <pre><code>In[36]:type(tweets_listdt[0]['id'])
  Out[37]:long
  </code></pre>
  
  <p>If I just use: </p>
  
  <pre><code>for tweet in tweets_listdt:
      tweetsdb.save(tweet)
  </code></pre>
  
  <p>The error mentioned above would happen.</p>
  
  <p>But if I add in this line, everything is okay:</p>
  
  <pre><code>tweet['id'] = int(tweet['id'])
  </code></pre>
  
  <p>And when I directly assign </p>
  
  <pre><code>tweets_listdtw = {'created_at': u'Sun Aug 03 17:07:24 +0000 2014',
   'id': 2704548373,
   'name': u'NoSQL',
   'text': u'RT @BigdataITJobs: Data Scientist'}
  </code></pre>
  
  <p><strong>tweetsdb.save(tweets_listdtw)</strong> is working, and </p>
  
  <pre><code>print type(tweets_listdtw['id'])
  &lt;type 'numpy.int64'&gt;
  </code></pre>
  
  <p>Got confused again... So definitely long type is okay...But why after I change 'id' into int, the saving is working?</p>