我设置了一个模式,如下所示:
employment = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref: "Users"
}
...
})现在,我通过在雇佣集合中搜索他们的userId来查询用户的雇佣情况。在用户模型和findById中保存employmentId引用会更快吗?或者差异不大?
感谢您的帮助,谢谢
发布于 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
https://stackoverflow.com/questions/52173942
复制相似问题