I’m having a discussion with one of my developers.
Here’s a useful snippet of relations:
Collection: ‘Property’
Property hasMany Spaces
Property hasMany Items
Spaces hasMany Items
Note that an Item subdocument may exist in a Property, and it may exist within a Space in a Property.
When this query is made, it is known that the item exists in a space, and the logic to determine that also easily provides the unique identifier for the Space.
My developer has written:
updateOne(
{
_id: propertyId,
'spaces.items.id': itemId
},
{
$set: {
'spaces.$[].items.$[item].<property>: value'
}
}
)
Can I know for certain that this query will look for one item, and after it finds it, stop looking? It seems to me that the query should go:
Find the property
Look in each space for an item with the given id
But I guess my developer thinks it would go:
Find the property
i = 0
Look in space[i].
Look in items
If item with itemId, update then return
Else i++
Thanks for any help.
Edit to add:
I notice that $ is actually called the “all positional operator” and:
indicates that the update operator should modify all elements in the specified array field.
https://www.mongodb.com/docs/manual/reference/operator/update/positional-all/#:~:text=Definition,-%24[]&text=The%20all%20positional%20operator%20%24[],in%20the%20specified%20array%20field.&text=For%20an%20example%2C%20see%20Update%20All%20Elements%20in%20an%20Array.
So in a battle of wills with my junior developer, would it be fair to say that AT BEST, his use of $ is a misuse since the intended usage of $ is to modify all elements in the specified array field?