I have this collection in MongoDB (version v3.6.3):
{
_id: '1'
elements:[
{
_id: '11'
status : DRAFT
},
{
_id: '12'
status : DRAFT
}
]
}
I would like to update the element 11
from job 1
ONLY IF the status is DRAFT
. I've tried this query:
db.getCollection('jobs').update(
{'_id':'1', 'elements._id': '11', 'elements.status': 'DRAFT'},
{'$set': { 'elements.$.status' : 'APPROVED' } }
)
and as result I have:
ambiguous positional update operation
what is the problem?
SOLUTION (thanks @neil-lun)
db.getCollection('jobs').update({
'_id':'1',
'elements': {
'$elemMatch': {
'_id': '11',
'elements.status': 'DRAFT'
}
}
},
{'$set': { 'elements.$.status' : 'APPROVED' } })
What confused me is that:
db.getCollection('jobs').find({
'_id':'1', 'elements._id': '11', 'elements.status': 'DRAFT'})
is a valid query and works fine