title: Check if an element exist in mongodB
body: <p>I want to check in an if statement if an array exists in dB. So far, I am checking the above statement in the cursor, but I am guessing that it slows down the query speed. My code until now is:</p>
  
  <p>EDIT:
       lines = [line.rstrip() for line in open(input_file)]</p>
  
  <pre><code>print len(lines)
  row_no = len(lines)
  col_no = len(lines)
  matrix = sparse.lil_matrix((len(lines), len(lines)))
  
  no_row  = 0
  counter = 0
  for item in lines:
      # find from database those items which their id exists in lines list and contain a follower_list 
      for cursor in collection.find({"_id.uid": int(item)}):
          if cursor['list_followers'] is None:
                  continue
          else:               
              id = cursor['_id']['uid']
              counter+=1
              print counter
              print id
              name = cursor['screenname']
              # text.write('%s \n' %name)
              followers = cursor['list_followers']    
              print len(followers)
              for follower in followers:
                  try:
                      if (follower in lines) and (len(followers)&gt;0):
                          matrix[no_row, lines.index(follower)] = 1
                          print no_row, " ", lines.index(follower), " ", matrix[no_row, lines.index(follower)]
                  except ValueError:
                      continue
              no_row+=1
              print no_row
  
  scipy.io.mmwrite(output_file, matrix, field='integer')  
  </code></pre>
  
  <p>Finally I discovered that the delay was due to the creation of the sparse.lil_matrix </p>