在 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')
有什么不同。是否找到像where这样的添加条件?还是控制返回的字段?
更新
好的,所以我看到 select 仅控制查询最终结果中的字段,但现在我不明白 Find
和 Where
有何不同。我是否无法使用 Find
和使用 Where
创建相同的查询?以下片段是否相同?
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);
最佳答案
Query#select(
arg
)Specifies which document fields to include or exclude
.select('name occupation')
表示结果应该只包含 name
和 occupation
字段。您不希望在结果中看到任何其他字段。
find
描述要在结果中包含哪些文档。 select
指示这些文档的哪些字段应在结果中可见。
关于node.js - 选择与查找以及在 Mongoose 中查找的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381247/