首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findById比findOne({userId: userId})快吗?

findById比findOne({userId: userId})快吗?
EN

Stack Overflow用户
提问于 2018-09-05 05:01:32
回答 1查看 604关注 0票数 0

我设置了一个模式,如下所示:

代码语言:javascript
复制
employment = new Schema({
  userId: {
    type: Schema.Types.ObjectId,
    ref: "Users"
  }
  ...
})

现在,我通过在雇佣集合中搜索他们的userId来查询用户的雇佣情况。在用户模型和findById中保存employmentId引用会更快吗?或者差异不大?

感谢您的帮助,谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-09-05 05:17:42

从性能的角度来看,我们拥有的文档是相同的: Definition of findById/** * Finds a single document by its _id field. `findById(id)` is almost* * equivalent to `findOne({ _id: id })`. If you want to query by a document's * `_id`, use `findById()` instead of `findOne()`. * * The `id` is cast based on the Schema before sending the command. * * This function triggers the following middleware. * * - `findOne()` * * \* Except for how it treats `undefined`. If you use `findOne()`, you'll see * that `findOne(undefined)` and `findOne({ _id: undefined })` are equivalent * to `findOne({})` and return arbitrary documents. However, mongoose * translates `findById(undefined)` into `findOne({ _id: null })`. https://github.com/Automattic/mongoose/blob/master/lib/model.js

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52173942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档