FindById быстрее, чем findOne ({userId: userId})? - PullRequest
0 голосов
/ 05 сентября 2018

У меня есть схема, подобная:

employment = new Schema({
  userId: {
    type: Schema.Types.ObjectId,
    ref: "Users"
  }
  ...
})

Сейчас я запрашиваю работу у пользователя, ища в коллекции вакансий его идентификатор пользователя. Будет ли быстрее сохранить ссылку jobId в пользовательской модели и findById? или разница незначительна?

Любая помощь приветствуется Спасибо

1 Ответ

0 голосов
/ 05 сентября 2018

Повторная документация у нас одинаковая с точки зрения производительности: Определение 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...