Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Query and Model mongoose

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.

like image 462
Alexander Pato Avatar asked May 17 '26 19:05

Alexander Pato


1 Answers

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.

like image 162
Saurabh Mistry Avatar answered May 22 '26 00:05

Saurabh Mistry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!