如何将旧的 Mongoose Schema 数据迁移到新的 Mongoose Schema 数据
How to migrate old Mongoose Schema data to new Mongoose Schema Data
我之前的 Mongoose Schema 是
social:{
instagram: String,
facebook: String,
whatsapp: String,
}
我的新猫鼬模式是
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
}
}
}
所以基本上在以前的模型中,我将 link 直接存储在变量中,例如 instagram、facebook、whatsapp
但是在新模型中,我将 link 存储在 main 变量下的数据变量中并添加了附加的活动变量,所以我如何以新的模式格式移动之前的大约 20 个数据
您可以执行一个 update
查询,将所有文档转换为新结构。你可以这样做:
db.collection.update({
"social.instagram.active": {
"$exists": false
}
},
[
{
"$set": {
"social": {
"instagram": {
"data": "$social.instagram",
"active": true
},
"facebook": {
"data": "$social.facebook",
"active": true
},
"whatsapp": {
"data": "$social.whatsapp",
"active": true
}
}
}
}
],
{
"multi": true
})
我之前的 Mongoose Schema 是
social:{
instagram: String,
facebook: String,
whatsapp: String,
}
我的新猫鼬模式是
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
}
}
}
所以基本上在以前的模型中,我将 link 直接存储在变量中,例如 instagram、facebook、whatsapp
但是在新模型中,我将 link 存储在 main 变量下的数据变量中并添加了附加的活动变量,所以我如何以新的模式格式移动之前的大约 20 个数据
您可以执行一个 update
查询,将所有文档转换为新结构。你可以这样做:
db.collection.update({
"social.instagram.active": {
"$exists": false
}
},
[
{
"$set": {
"social": {
"instagram": {
"data": "$social.instagram",
"active": true
},
"facebook": {
"data": "$social.facebook",
"active": true
},
"whatsapp": {
"data": "$social.whatsapp",
"active": true
}
}
}
}
],
{
"multi": true
})