set field for docs in one collection based on ref to another collection

Viewed 21

I have todo schema and actions schema. Each todo is made up of actions. My todos already have a field for 'subject'.

Question: How do I update the actions so that their 'subject' field equals a given 'subject' field fro the referenced todo?

const todo = new Schema({
  subject: String,
  actions: { type: Schema.Types.ObjectId, ref: 'Action' },


})
const action = new Schema({
  action: String ,
  todoID: { type: Schema.Types.ObjectId, ref: 'Todo' },
  subject: String //needs to be populated

})

*I want to avoid doing something like this: Action.find().forEach(action=>{ todo.findByID(action.todoID) .then(yadda yadda) .catch(not what I want to do) })

I feel like there is an aggregation solution but I cant figure this out.

Thank you.

0 Answers
Related