My previous Mongoose Schema is
social:{
instagram: String,
facebook: String,
whatsapp: String,
}
and my new Mongoose Schema is
social:{
instagram: {
data: String,
active: {
type: Boolean,
default: true
}
},
facebook:{
data: String,
active: {
type: Boolean,
default: true
}
},
whatsapp:{
data: String,
active: {
type: Boolean,
default: true
}
}
}
so basically in previous model I am storing link directly in variable like instagram,facebook,whatsapp
but in new model , I am storing link in data variable under main variable and adding addtional active variable , so how can I move previous around 20 data in new format of schema
CodePudding user response:
You can get all of the documents with Modal.find({})
and then you can loop through them where you will do something like:
let modal = new Social();
modal.social.instagram.data = existingDocument.social.instagram;
modal.social.facebook.data = existingDocument.social.facebook;
modal.social.whatsapp.data = existingDocument.social.whatsapp;
modal.save();
As you set a default value for the active
field that will be created automatically. Then you can delete the old docs or whatever you want to do with them.
CodePudding user response:
You can do one update
query that will transfer all documents to new structure. You can do it like this:
db.collection.update({},
[
{
"$set": {
"social": {
"instagram": {
"data": "$social.instagram",
"active": true
},
"facebook": {
"data": "$social.facebook",
"active": true
},
"whatsapp": {
"data": "$social.whatsapp",
"active": true
}
}
}
}
],
{
"multi": true
})