我有一个模式设置,如下所示:
employment = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref: "Users"
}
...
})
现在,我正在通过搜索其用户ID的就业集合来查询用户的就业。将 employmentId 引用保存在用户模型中并查找 ById 会更快吗?还是差异微不足道?
任何帮助不胜感激谢谢
从性能的角度来看,我们拥有的文档是相同的: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