Mongo DB 4.0
I want to update all elements of an array of an document by concatenate fields I have following document:
{
"id": 123455,
"articles": [
{
"country": "DE",
"articleNumber": "123456789"
},
{
"country": "AT",
"articleNumber": "987654321"
}
]
}
and I need extends article with a new field which is concatenate by to other fields
{
"id": 123455,
"articles": [
{
"country": "DE",
"articleNumber": "123456789",
"countryArticleNumber": "DE123456789"
},
{
"country": "AT",
"articleNumber": "987654321",
"countryArticleNumber": "AT987654321"
}
]
}
I tried several solutions I found here in Stackoverflow like:
db.articleDocument.update (
{_id: ObjectId('5f15414ff3812f4838391edd')},
{
$push:
{'articles.countryArticleNumber': 'dasd'}
}
)
but even with a simple update I got
MongoWriteException: Cannot create field 'countryArticleNumber' in element {articles: [ {
I also tried with db.articleDocument.updateOne, $set instead of $push, positional operator
I was expecting something like this should work
db.articleDocument.updateOne(
{_id: ObjectId('5f15414ff3812f4838391edd')},
{
$set:
{
"articles.$.countryArticleNumber": {
$concat: ["articles.$.country", "articles.$.articleNumber"]
}
}
}
)
but
The positional operator did not find the match needed from the query.
Thx in advanced