node.js - 选择与查找以及在 Mongoose 中查找的位置

标签 node.js mongodb 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') 有什么不同。是否找到像where这样的添加条件?还是控制返回的字段? 更新 好的,所以我看到 select 仅控制查询最终结果中的字段,但现在我不明白 FindWhere 有何不同。我是否无法使用 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);

最佳答案

来自API docs on select :

Query#select(arg)

Specifies which document fields to include or exclude

.select('name occupation') 表示结果应该只包含 nameoccupation 字段。您不希望在结果中看到任何其他字段。

find 描述要在结果中包含哪些文档。 select 指示这些文档的哪些字段应在结果中可见。

关于node.js - 选择与查找以及在 Mongoose 中查找的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381247/

相关文章:

javascript - 处理数据并记录来自请求的人

javascript - 在 Node 中使用箭头函数时 Arguments 对象的行为不正确

javascript - 无法掌握回调的时机

javascript - 构建开发失败 : Cannot set property 'fileSystem' of null

c# - MongoVue 和 Guid

node.js - db.collection.insert 与 db.collection.insertOne 和 db.collection.insertMany 的性能影响

mongoose - 架构数组路径的值无效

javascript - Mongoose 错误 : Operation 'featureds.find()` buffering timed out after 10000ms

node.js - 通过对象数组查找文档

javascript - 不在 Mongoose 模式中的嵌套对象上的点表示法