i have a document structure like this:
{
"_id": "...",
"shop": "pepe",
"fruits": [
{
"name": "banana",
"taste": "sweet",
},
]
}
Now i want to change the type of fruits.taste to an array like this: "taste": ["sweet"]
So the result should be this:
{
"_id": "...",
"shop": "pepe",
"fruits": [
{
"name": "banana",
"taste": ["sweet"],
},
]
}
i was trying to do that like this in a mongo playground query but that does not work:
db.getCollection('shops')
.find()
.forEach(function(document) {
db.shops.update(
{ _id: document._id },
{ $set: { "document.fruits.$[].taste": [document.fruits.$.taste] } }
);
})
How can i change in objects, which are in an array, a value from string to an array of string ?
Thanks for your help!
2
Answers
Query
$taste
so you need a pipeline updatefruts
, and put thetaste
value inside[]
Playmongo
Here’s one way you could do it by using a pipeline in the
update
.Try it on mongoplayground.net.