I need to update a MongoDB document that looks like this:
{
"_id" : ObjectId("A"),
"participant" : "John Doe",
"roles" : [{
"roleId" : ObjectId("AA"),
"responsibilities" : [{
"_id" : ObjectID("AAA"),
"name" : "resp 1"
}, {
"_id" : ObjectID("AAB"),
"name" : "resp 2"
}]
}, {
"roleId" : ObjectId("AB"),
"responsibilities" : []
}]
}
The updates come in two forms:
- Remove all role sub-documents that have zero responsibilities... this should remove role sub-document with _id = AB
- Add new role if it does not already exist:
{ "roleId" : ObjectId("AC"), "responsibilities" : [] }