L
L
low molecular macro2021-07-24 19:07:04
MongoDB
low molecular macro, 2021-07-24 19:07:04

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;


If I use 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

1 answer(s)
R
Rag'n' Code Man, 2021-07-25
@iDmitriyWinX

find()in 100% of cases it returns an array ( Document[])
findOnein 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 question

Ask a Question

731 491 924 answers to any question