how can i achieve this query but using mongoose?
SELECT password FROM users WHERE email = "[email protected]";
i want to find a password in the DB for a specified email, thanks in advance
CodePudding user response:
Use query to find the object you want and projection to return the desired value. Check the docs
query: Optional. Specifies selection filter using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}).
projection: Optional. Specifies the fields to return in the documents that match the query filter. To return all fields in the matching documents, omit this parameter
By the way you need something like this:
users.findOne({
"email": "[email protected]"
},
{
"_id": 0,
"password": 1
})
The query is the first object, is like the WHERE in SQL. And projection the second object and is like SELECT, is where you say mongo "give me only the password field... and also don't give me _id" (by default _id is always given).
Example here
Note that in the query I've used users.findOne but it depends on your model name and the query you want.
