title: Python db Not equal Mongo Code not Working?
body: <p>The following works when typed directly in the mongodb shell—I receive the correct output:</p>
  
  <pre><code>db.serial_key.find({key: {$ne : "5SNT0V"}})
  </code></pre>
  
  <p>However, in Python 3, it's not working. Every time, only the if block runs. It does not matter if I use <code>str</code> or not in the query.</p>
  
  <pre><code>    for x in keys:
           i +=1;
           print('key %d  = ' %i ,  keys[i-1])  #out put: key 1  =  3ZOHSH
  
           # place values in dictionary
           keyrecord_record = {'key':keys[i-1],'b_p_code':x1}
  
  
           if(db.serial_key.find({'key':{ '$ne':str(keys[i-1])}})):
               db.insert(keyrecord_record)
           else:
               print('Record in DB')
  </code></pre>
  
  <p>Please, I expect some expert help. I'm trying to do this within one day. I only want to write values that are not already in the database.</p>
  
  <p>I found this question: 
  <a href="https://stackoverflow.com/questions/23326687/mongo-db-not-equal-to-query-not-working">Mongo db not equal to query not working</a></p>
  
  <p>...but it does not answer my question.</p>
  
  <p>===================full main class code==========================</p>
  
  <pre><code>from pymongo import MongoClient
  from algo import algo
  
  
  #take in put data
  x1 = int(input("Enter a number(Brand_name+Pack_size) : "))
  y1 = int(input("Enter a number: key Quntity "))
  
  #create connection
  client = MongoClient()
  
  #create emty list
  alist = []
  
  #make database_table
  db = client.product.serial_key
  keyrecord_record = {}
  
  #find table existing entry that code
  f_cursor = db.find({"b_p_code": x1})
  
  for document in f_cursor:
      alist.append(document['key'])
      #print(document['key']) //print expected result
  
  
  
  if(x1 == ""  or y1==""):
      print("please enter valid no")
  else:
      x = algo(x1,y1,alist)
  
      keys =x.id_generator()
      keys.sort(reverse=True)
      print('\n')
      i=0;
      for x in keys:
          i +=1;
          print('key %d  = ' %i ,  keys[i-1])
  
          # place values in dictionary
          keyrecord_record = {'key':keys[i-1],'b_p_code':x1}
  
  
          #not recived expected result. if key in database again print key
          if(db.serial_key.find({'key':{ '$ne':str(keys[i-1])}})):
              db.insert(keyrecord_record)
          else:
              print('Record in DB')
  
  
      print("\n")
      print("Batch No: come from db ")
      print('Generate Key beetween  %d00000 - %d00000'  % (x1 ,(x1+1)) )
      print("Startin Serial : " , keys[0])
      print("Ending  Serial : " , keys[len(keys)-1])
  
  
  
  print('\n      Database Details   \n')
  #print details
  cursor = db.find()
  
  for document in cursor:
      print(document['key'])
      #print(document)  
  </code></pre>
  
  <p>Image showing console out put.</p>
  
  <p><a href="https://i.stack.imgur.com/RUGVC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RUGVC.png" alt="enter image description here"></a></p>