Sync Project Task Matrix and Recalculate Milestones
You manage a project tracking database where each project contains an array of milestones, and each milestone contains an embedded array of tasks. You need to perform a complex maintenance operation across all active projects. First, for every project where the status is 'in_progress', find all milestones that have an array of tasks containing at least one task marked with priority: 'high' and completed: false. For each of these matching milestones, push a new warning task into its tasks array with the title: 'Audit Required', priority: 'urgent', and completed: false, but only if a task with that exact title does not already exist in that milestone's tasks array. Second, update the project level last_audit_date to the fixed ISODate('2024-06-01T00:00:00Z') for all affected projects.### Collections#### projects - _id: Integer - name: String - status: String ('in_progress', 'completed', 'archived') - last_audit_date: Date or null - milestones: Array of embedded documents containing: - milestone_id: Integer - title: String - tasks: Array of embedded documents containing: - task_id: Integer - title: String - priority: String ('low', 'medium', 'high', 'urgent') - completed: Boolean### TaskExecute a single or multi-stage update operation on the projects collection using updateMany that targets projects with status: 'in_progress', adds the audit task to the relevant milestones using array update operators and positional operators without duplication, and sets last_audit_date.### ExampleOn the visible dataset, running this update will modify projects that have in_progress status and meet the task criteria.
| _id | last_audit_date | milestones | name |
|---|---|---|---|
| 1 | 2024-06-01T00:00:00.000Z | [{"tasks":[{"priority":"high","title":"Wireframe"},{"priority":"low","title":"Logo"},{"priority":"urgent","title":"Au… | Alpha |
| 2 | NULL | [{"tasks":[{"priority":"high","title":"Deploy"}],"title":"Launch"}] | Beta |
Submitting also runs your answer against 2 hidden datasets, each built around an edge case: NULLs, ties, empty tables. A failure names the case without showing its data.
Follow-up: How would you structure this update if you also needed to increment a counter field tracking how many times milestones have been audited?
Use standard MongoDB update methods. Do not use JavaScript functions or $where. Ensure that duplicate audit tasks are not inserted if the title 'Audit Required' is already present in the milestone tasks array.