4

these days i'm trying to learn more about mongoose to implement it in my project ,while going through the doc , i saw both Model and Query, both have many methods in common ,my question is what is the difference between them for example

Model.findOne()vs Query.prototype.findOne(), and thank you in advance.

1 Answers1

1

Model.findOne() is the actual way to finding single document from database using findOne() query

whereas Query.prototype.findOne() means prototype of findOne Query

as per documentation : you can pass your filter, projection, options objects and callback function to your query

example :

 Kitten.where({ color: 'white' }).findOne(function (err, kitten) {
      if (err) return handleError(err);
      if (kitten) {
        // doc may be null if no document matched
      }
 });

mongoose have displayed prototypes of all Query you can use to your Model , means what you can pass to your Query while finding documents.

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • i guess `prototype` is related to inheritance, and not what you meant here. [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype) – Medi Nov 22 '18 at 20:01