如何在不覆盖当前数据的情况下更新 Mongoose 中的混合类型字段?
How to update mixed type field in Mongoose without overwriting the current data?
我有以下架构
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ShopSchema = new Schema({
name: Schema.Types.Mixed,
country: {
type: String,
default: ''
},
createdAt: {
type: Date,
default: Date.now
},
defaultLanguage: {
type: String
},
account: {type : Schema.ObjectId, ref : 'Account'},
});
mongoose.model('Shop', ShopSchema);
"name" 字段是多语言的。我的意思是,我会保留多语言数据,例如
name: {
"en": "My Shop",
"es": "Mi Tienda"
}
我的问题是,在控制器中,我正在使用此代码更新商店:
var mongoose = require('mongoose')
var Shop = mongoose.model('Shop')
exports.update = function(req, res) {
Shop.findByIdAndUpdate(req.params.shopid, {
$set: {
name: req.body.name
}
}, function(err, shop) {
if (err) return res.json(err);
res.json(shop);
});
};
很明显,新数据覆盖了旧数据。我需要的是用新数据扩展旧数据。
有什么方法可以做到吗?
对特定元素使用 "dot notation":
Shop.findByIdAndUpdate(req.params.shopid, {
"$set": {
"name.en": req.body.name
}
}, function(err, shop) {
if (err) return res.json(err);
res.json(shop);
});
});
这将只覆盖 "en" 元素(如果这是您想要的),或者 "create" 一个包含您设置的数据的新元素。因此,如果您使用 "de" 而它不存在,则会有其他元素和一个具有值的新 "de" 元素。
您应该使用方法 .markModified()。请参阅文档 http://mongoosejs.com/docs/schematypes.html#mixed
Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.
person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved
我有以下架构
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ShopSchema = new Schema({
name: Schema.Types.Mixed,
country: {
type: String,
default: ''
},
createdAt: {
type: Date,
default: Date.now
},
defaultLanguage: {
type: String
},
account: {type : Schema.ObjectId, ref : 'Account'},
});
mongoose.model('Shop', ShopSchema);
"name" 字段是多语言的。我的意思是,我会保留多语言数据,例如
name: {
"en": "My Shop",
"es": "Mi Tienda"
}
我的问题是,在控制器中,我正在使用此代码更新商店:
var mongoose = require('mongoose')
var Shop = mongoose.model('Shop')
exports.update = function(req, res) {
Shop.findByIdAndUpdate(req.params.shopid, {
$set: {
name: req.body.name
}
}, function(err, shop) {
if (err) return res.json(err);
res.json(shop);
});
};
很明显,新数据覆盖了旧数据。我需要的是用新数据扩展旧数据。
有什么方法可以做到吗?
对特定元素使用 "dot notation":
Shop.findByIdAndUpdate(req.params.shopid, {
"$set": {
"name.en": req.body.name
}
}, function(err, shop) {
if (err) return res.json(err);
res.json(shop);
});
});
这将只覆盖 "en" 元素(如果这是您想要的),或者 "create" 一个包含您设置的数据的新元素。因此,如果您使用 "de" 而它不存在,则会有其他元素和一个具有值的新 "de" 元素。
您应该使用方法 .markModified()。请参阅文档 http://mongoosejs.com/docs/schematypes.html#mixed
Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.
person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved