title: Check if entry exists in MongoDB using Python Flask
body: <p>I have a collection of "rooms" which contains a collection of "users." I am trying to search the "rooms" collection to see if a username already exists, and if it does, update the "user_ip" field. The collection looks like:</p>
  
  <pre><code>{
      '_id': ObjectId('59eea13a2728d50a5edefe0z'), 
      'name': 'RH-0', 
      'room_key': 'test123', 
      'users': 
          [{'username': 'Pablo', 'user_ip': '127.0.0.1', 'user_port':0}, 
           {'username': 'Franco', 'user_ip': '127.0.0.1', 'user_port':1}, 
           {'username': 'Valeri', 'user_ip': '127.0.0.1', 'user_port':2}, 
           {'username': 'Sarah', 'user_ip': '127.0.0.1', 'user_port':3}
          ], 
      'workshop_unit': 'wsu0'
  }
  </code></pre>
  
  <p>My current solution attempts to use the roomname and username, but I would prefer to use just the "username" to search.</p>
  
  <pre><code>def user_exists(roomname, username):
  if db.rooms.find({"name": roomname},{'username': username}).count() &gt; 0:
      return 1
  else:
      return 0
  </code></pre>
  
  <p>**Note: The username will be a unique one throughout all the collections.</p>