Answer the question
In order to leave comments, you need to log in
Difference in findOne and find results?
There is a request like this:
let query = User.findOne();
query = query.or([
{ username: incomingData.username},
{ email: incomingData.email},
]);
query.projection("username email");
const result = await query;
find()
, then I can work directly with result without using . result will be an empty array if nothing is found. Or an array with found elements. Therefore, I can write further
. But in the case of working with the search result, I can only work through
Why so? await query.then(result => {})
if (result.length == 0)
findOne()
await query.then(result => {})
Answer the question
In order to leave comments, you need to log in
find()
in 100% of cases it returns an array ( Document[]
)
findOne
in 100% of cases it returns 1 element ( Document
)
Therefore, when using find, we must go through a loop if we want to work with each found answer individually.
And your question is the opposite.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question