3

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.

4

1 回答 1

1

Model.findOne()是使用 findOne() 查询从数据库中查找单个文档的实际方法

Query.prototype.findOne()表示 findOne 查询的原型

根据文档:您可以将过滤器、投影、选项对象和回调函数传递给您的查询

例子 :

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

mongoose 已经展示了您可以用于 Model 的所有Query的原型,这意味着您在查找文档时可以传递给 Query 的内容。

于 2018-11-22T19:47:32.960 回答