'Using sprinboot convert existing document's inner object to an array
I have below structure in MongoDB :
{
"id" : "6c71-fea4-4bg6-a0bf-768n0n54t59",
"continents" : [
{
"id" : "45" ,
"countries" : [{
"id" : "3e4",
"states" : {
"id" : 28,
"name" : "string"
}
}
]
}
]
}
,
Requirement is to update this state attributes as an array from object:
{
"id" : "6c71-fea4-4bg6-a0bf-768n0n54t59",
"continents" : [
{
"id" : "45" ,
"countries" : [ {
"id" : "3e4",
"states" : [{
"id" : 28,
"name" : "string"
} ]
] }
}
]
}
Mongock changelog that I tried:
@ChangeLog(order = "0020")
class DBChangeToArray {
@ChangeSet(author = "me", order = "0020", id = "updatearray")
fun convertObjectToArray(mongockTemplate: MongockTemplate) {
Update().set("continents.countries.states", Query())
.let { updateOperation ->
mongockTemplate.updateMulti(
Query().addCriteria(
Criteria.where("_id").`is`("6c71-fea4-4bg6-a0bf-768n0n54t59")
), updateOperation, Collection::class.java
)
}
}
}
Since I have to do it from springboot service,I was trying to use mongock to do the same with Update.set operator,but its not working.Please help,even plain mongo query would help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
