A
A
Alexander Buki2020-06-02 18:06:18
Mongoose
Alexander Buki, 2020-06-02 18:06:18

How to search where nested array elements should have a margin?

There is a homework plan:

const schema = new Schema({
  dateAssign: { type: Date, default: Date.now, index: true },
  teacher: { type: Schema.ObjectId, ref: 'user', index: true },
  lesson: { type: Schema.ObjectId, ref: 'lesson', index: true },
  exercises: [
    { type: Schema.ObjectId, ref: 'exercise' },
  ],
});
mongoose.model('homework', schema)

exercise scheme:
const schema = new Schema({
  block: { type: Schema.ObjectId, ref: 'lessonblock', index: true },
  variant: { type: Schema.ObjectId, ref: 'variant', index: true },
  result: { type: Schema.ObjectId, ref: 'exerciseresult', index: true },
  variables: { type: Schema.Types.Mixed, default: {} },
  start_at: { type: Date },
  lock: { type: Boolean, default: false, index: true },
});
mongoose.model('exercise', schema);

It is necessary to find all the objects of the schema c homeworks such that in the nested array exercises all the elements of the array had the field lock = true;

I tried this, it doesn't work:
const HWs = await Homework.find({ teacher, 'exercises.lock': true }, null, { sort: { dateAssign: -1 }, limit: 20 })

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question