title: How can I check if something already exists in a MongoDB database?
body: <p>I've tried to learn MongoDB [and more specifically pymongo] and I'm trying to use it in conjunction with discord.py to make an inventory system for a bot. The bot fetches a userID and should then compare it in the database, so that it can append their &quot;inventory&quot; if they are already in it, instead of making an entirely new one.</p>
  <p>However, every solution I've tried so far always seems to fail - no matter if a userID is in the database or not, it'll just create a new entry in the database with the same userID.</p>
  <p>Solutions I've tried so far:</p>
  <pre><code>userID = ctx.message.author.id
  if mycol.collection.count_documents({ 'userID': userID }, limit = 1):
      await ctx.send(&quot;**Adding new inventory to the database**&quot;)
      mydict = { &quot;userID&quot;: userID, &quot;coin&quot;: &quot;0&quot;, &quot;inv&quot;: {&quot;inv1&quot;: &quot;&quot;, &quot;inv2&quot;: &quot;&quot;, &quot;inv3&quot;: &quot;&quot;, &quot;inv4&quot;: &quot;&quot;, &quot;inv5&quot;: &quot;&quot;} }
      y = mycol.insert_one(mydict)
  else:
      await ctx.send(&quot;**Error: You're already in the database**&quot;)
  </code></pre>
  <hr />
  <pre><code>userID = ctx.message.author.id
  result = mydb.find({&quot;userID&quot;: userID}) 
      if result.count() &gt; 0:  
          await ctx.send(&quot;**Error: You're already in the database**&quot;)
      else:
          await ctx.send(&quot;**Adding new inventory to the database**&quot;)
          mydict = { &quot;userID&quot;: userID, &quot;coin&quot;: &quot;0&quot;, &quot;inv&quot;: {&quot;inv1&quot;: &quot;&quot;, &quot;inv2&quot;: &quot;&quot;, &quot;inv3&quot;: &quot;&quot;, &quot;inv4&quot;: &quot;&quot;, &quot;inv5&quot;: &quot;&quot;} }
          y = mycol.insert_one(mydict)
  </code></pre>
  <hr />
  <pre><code>userID = ctx.message.author.id
  search = mycol.find({'userID': userID})
      if search == True:
              await ctx.send(&quot;**Error: You're already in the database**&quot;)
          else:
              await ctx.send(&quot;**Adding new inventory to the database**&quot;)
                      mydict = { &quot;userID&quot;: userID, &quot;coin&quot;: &quot;0&quot;, &quot;inv&quot;: {&quot;inv1&quot;: &quot;&quot;, &quot;inv2&quot;: &quot;&quot;, &quot;inv3&quot;: &quot;&quot;, &quot;inv4&quot;: &quot;&quot;, &quot;inv5&quot;: &quot;&quot;} }
              y = mycol.insert_one(mydict)
  </code></pre>
  <p>Is there something I'm doing wrong? Are these solutions just out of date? Any and all help is appreciated.</p>
  <p>[Original solutions: https://stackoverflow.com/questions/25163658/mongodb-return-true-if-document-exists ]</p>