How to query and update nested arrays

I am building a course system. Each course has several sections, each section has several stages. My data structure is as follows:

{ "_id" : "Mtz4DMTwMMKWTWbzE", "slug" : "how-to-be-awesome", "title" : "How to be awesome", "description" : "In 4 easy lessons.", "createdAt" : ISODate("2014-08-25T13:33:24.675Z"), "sections" : [ { "title" : "Be cool", "description" : "Title says it all really", "steps" : [ { "title" : "Wear sunglasses", "description" : "Always works." }, { "title" : "Be funny", "description" : "Make an occasional joke. But no lame ones." } ] } ] } 

This worked when adding steps;

 Course._collection.update( { _id: course._id, sections: section }, { "$push": { "sections.$.steps": step } }) 

But I can’t figure out how to update the step. I tried to give the ID steps and do it like this, but it does not work, apparently because it is two arrays in depth and you cannot have two positions ( $ ) in the request. I tried something like this:

 Course._collection.update( { _id: course._id, 'sections.steps._id': step._id }, { "$set": { "sections.steps.$.title": "test updated title" } }) 

But this gave the following error:

cannot attach to array using string field name: steps

Is there any way to do this? Or is my circuit design disabled?

Thanks!

0
source share

Source: https://habr.com/ru/post/1203607/


All Articles