Answer the question
In order to leave comments, you need to log in
How can I change the data type of a String field to [String] in MongoDB while retaining the previous data?
I have a field with phone number phone
. Currently, only one phone number can be stored there. But now we need to store several phone numbers, and I need to somehow write a migration for all this.
I tried
db.Clients.updateMany({}, {$set: { phones: ["$phone"] } })
phone
, he simply substituted this string there.
Answer the question
In order to leave comments, you need to log in
db.Clients.find({}).forEach(el => {
db.Clients.updateOne({ _id: el._id }, { $set: { phone: [el.phone] }})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question