Kaushik Makwana Kaushik Makwana - 1 year ago 100
Python Question

How to select a single field in MongoDB using Pymongo?

I'm trying to find a record within MongoDB, and filter

from the result.

Here is my code:

@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == "POST":
password = request.form.get('password')
email = request.form.get('email')
db = get_db()
data = db.author.find_one({'email' : email, 'password' : password})
return 'data'
return render_template('login.html')


{'password': '123123', 'name': '<my_name>', 'email': '<my_email>', '_id': ObjectId('<an_object_id_string>')}

How do I filter the
field from the output?

Answer Source

You need to specify the field you want to return using projection.

data = db.author.find_one({'email' : email, 'password' : password}, {'_id': 1})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download