Mongoose, ссылка на элемент в массиве - PullRequest
0 голосов
/ 14 июня 2019
let itemSchema = new Schema({
  description: String,
  evaluations: [
    {
      evaluation: {
        type: Schema.Types.ObjectId,
        ref: 'evaluation',
        required: true,
        index: true
      },
      selection: {
        type: Schema.Types.ObjectId,
        ref: '????', // How do i reference to 'selection' field here?
        required: true,
        index: true
      }
    }
  ]

let evaluationSchema = new Schema({
  name: {type: String, required: true},
  selections: [
    {
      name: {type: String, required: true }, 
      value: { type: Number, min: 0, max: 15 },
    }
  ],
});

Можно ли сделать ссылку на элемент в массиве выборок? Как написан этот синтаксис? Не могу найти в документах.

1 Ответ

0 голосов
/ 14 июня 2019

Пожалуйста, укажите имя коллекции модели сначала для обеих схем, а затем в ref поместите имя коллекции AssessationSchema,

Другое, вы не можете ссылаться на поле, вы можете только ссылаться на схему (сделать ссылку на поле можно)в агрегации mongodb с $ project)

let itemSchema = mongoose.Schema({
  description: String,
  evaluations: [
    {
      evaluation: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Eval',
        required: true,
        index: true
      },
      selection: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Eval', // How do i reference to 'selection' field here?
        required: true,
        index: true
      }
    }
  ]
});

mongoose.model("Item", itemSchema);

let evaluationSchema = mongoose.Schema({
  name: {type: String, required: true},
  selections: [
    {
      name: {type: String, required: true }, 
      value: { type: Number, min: 0, max: 15 },
    }
  ],
});
 mongoose.model("Eval", evaluationSchema);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...