I have a collection like this:
readers
[{
"id": 123,
"name": "John Doe",
"books": [
{
"type": "comic",
"names": [
"batman",
"tintin"
]
},
{
"type": "drama",
"names": [
"hamlet",
"otelo"
]
}
]
},
{...}]
Then I want to update my collection adding in array names
matching by type
.
When type
exists works perfectly, in this example romeo and juliet
is pushed correctly:
db.collection('readers').updateOne(
{ id: 123 },
{
$push: {
'books.$[element].names': 'romeo and juliet'
}
},
{
upsert: true,
arrayFilters: [{ 'element.type': 'drama' }] // type exists
}
);
The problem come when type
is not matched:
db.collection('readers').updateOne(
{ id: 123 },
{
$push: {
'books.$[element].names': 'hobbit'
}
},
{
upsert: true,
arrayFilters: [{ 'element.type': 'fantasy' }] // new type
}
);
I expect this result but nothing is added:
{
"id": 123,
"name": "John Doe",
"books": [
{
"type": "comic",
"names": [
"batman",
"tintin"
]
},
{
"type": "drama",
"names": [
"hamlet",
"otelo"
]
},
{
"type": "fantasy",
"names": [
"hobbit"
]
}
]
}
What am I doing wrong?