mardi 5 mars 2019

MongoDB change type of a field [duplicate]

This question already has an answer here:

In MongoDB, I wanted to change the type of a field info in collection customer_info-2019 from string to json object.

So I wrote script:

db.getCollection("customer_info-2019").find({}, {info:1}).forEach(function(doc){
doc.info=JSON.parse(doc.info);
db.getCollection("customer_info-2019").save(doc);})

It did the conversion fine, but it also deleted all of other fields and only kept the info field. How should I modify this script so it does the conversion for multiple fields and also don't delete any other fields.




Aucun commentaire:

Enregistrer un commentaire