Reputation: 874
I have the following structure:
"roles": [
{
"aspirants": ["Will Smith"],
"description": {
"content": "Role 1 content",
"title": "Role 1"
},
"quantity": 12,
"remunaration": 950
},
{
"aspirants": ["Bruce Wayne"],
"description": {
"content": "Role 2 content",
"title": "Role 2"
},
"quantity": 6,
"remunaration": 1000
}
]
So, what I need to do is to add a new aspirant matching the role he is postulating (that data is coming from the UI). For example, if "Tony Stark" is postulating to the "Role 1", then I have to update that field, so it will end as follows:
"roles": [
{
"aspirants": ["Will Smith", "Tony Stark"],
"description": {
"content": "Role 1 content",
"title": "Role 1"
},
"quantity": 12,
"remunaration": 950
},
{
"aspirants": ["Bruce Wayne"],
"description": {
"content": "Role 2 content",
"title": "Role 2"
},
"quantity": 6,
"remunaration": 1000
}
]
Any idea how can I achieve this? Of course, the roles are a field from a document.
Upvotes: 1
Views: 41
Reputation: 8894
You can use with arrayFilters
db.collection.update(
{},
{$addToSet:{'roles.$[r].aspirants':"Spider man"}},
{ arrayFilters: [{ 'r.description.title': 'Role 1' }], new: true }
)
Upvotes: 1