selectvsfind和在哪里find在Mongoose
在Mongoose文档中有这个小片段:
Person .find({ occupation: /host/ }) .where('name.last').equals('Ghost') .where('age').gt(17).lt(66) .where('likes').in(['vaporizing', 'talking']) .limit(10) .sort('-occupation') .select('name occupation') .exec(callback);
我很难理解.find({ occupation: /host/ })
与.select('name occupation')
.find({ occupation: /host/ })
不同的.select('name occupation')
。 是否find像在哪里添加条件? 还是它控制返回的字段? 更新好了,所以我看到select只控制查询的最终结果的字段,但现在我不明白如何Find
和Where
是不同的。 我不能使用Find
和使用Where
创build相同的查询? 下面的代码片段是一样的吗?
Person .where('occupation').equals('host') .where('name.last').equals('Ghost') .where('age').gt(17).lt(66) .where('likes').in(['vaporizing', 'talking']) .limit(10) .sort('-occupation') .select('name occupation') .exec(callback);
find
是实际的查询。 在这个例子中,你得到的所有行的occupation
等于host
。 现在,每个匹配该查询的对象都有几个属性。 让我们假设它有属性name
, age
, email
和occupation
。 当你指定你想select
name
和occupation
你说你只是想要这些属性。 所以在我们的情况下, age
和email
将不会从查询发回。
在这种情况下用于指定多个查询的约束条件。 通常, where
使用,因为它比提供更大的灵活性,比如在javascriptexpression式中传递
从select
的API文档 :
查询
arg
(arg
)指定要包含或排除的文档字段
.select('name occupation')
表示结果应该只包含name
和occupation
领域。 您不希望在结果中看到其他字段。
find
描述哪些文件包含在结果中。 select
指示这些文档的哪些字段应该在结果中可见。