I need to update the dateP in the following structure with "2022年01月02日", but it seems not an easy task:
{
"_id": ObjectId("5c05984246a0201286d4b57a"),
"_a": [
{
"_p": {
"s": {
"a": {}
}
}
},
{
"_onlineStore": {}
},
{
"_p": {
"s": {
"a": {
"t": [
{
c: 4
},
{
"dateP": "20200-09-20",
"l": "English",
"size": "XXL"
},
{
c: 1
}
]
},
c: {
t: 2
}
}
}
}
]
}
I attempted with arrayFilters, but without success as not all elements exist in all documents and some documents are pretty empty. Please advice.
MongoDB 4.2 community
1 Answer 1
Believe that you need the filtered positional operator for _a array to check whether the document has the _p field or not.
db.collection.update({},
{
$set: {
"_a.$[a]._p.s.a.t.$[x].dateP": "2022年01月02日"
}
},
{
arrayFilters: [
{
"a._p": {
$exists: true
}
},
{
"x.dateP": "20200-09-20"
}
]
})
answered Oct 12, 2022 at 13:16
Yong Shun
54.8k6 gold badges38 silver badges66 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
nimrod serok
This is a very nice solution!
R2D2
indeed I like it too but not working when there is not exisiting elements in the path ... :( , @Yong Shun , please , help with the fix and I will accept your answer.
nimrod serok
How about this? - just a small change
Yong Shun
Nice @nimrodserok, this works better in one line, rather than in my comment.
R2D2
one more similar question if somebody interested: stackoverflow.com/questions/74043251/…
Explore related questions
See similar questions with these tags.
lang-js
{ $exists: true }😂