我从角度
获得此数据 {
"name": "Test name",
"conditions": [
{
"id": "56a53ba04ce46bf01ae2bda7",
"name": "First condition"
},
{
"id": "56a53bae4ce46bf01ae2bda8",
"name": "Second condition"
}
],
"colors": [
{
"id": "56a545694f2e7a20064f228e",
"name": "First color"
},
{
"id": "56a5456f4f2e7a20064f228f",
"name": "Second color"
}
]
}
我希望将它保存在mongoDb的ProductSchema中,但我不知道如何为此创建架构
var ProductSchema = new Schema({
name: String,
conditions: [Array],
colors: [Array]
});
以及它如何保存到服务器控制器中的模型
var product = new Products({
name: req.body.name,
conditions: req.body.conditions,
colors: req.body.colors
});
当我使用这个Schema和这个控制器时,我得到除了name和ObjectId之外的集合中的空记录。如何创建正确的架构和控制器?
答案 0 :(得分:8)
创建架构后,您需要创建一个猫鼬模型,然后您可以保存它。尝试这样的事情:
var ProductSchema = new Schema({
name: String,
conditions: [{}],
colors: [{}]
});
var Product = mongoose.model('Product', productSchema);
var product = new Product({
name: req.body.name,
conditions: req.body.conditions,
colors: req.body.colors
});
product.save( function(error, document){ //callback stuff here } );
答案 1 :(得分:0)
当然,以上答案有效,请将其视为替代。
var defaultArray = ["A","B","C"];
var schemaDef = mongoose.Schema(
permission: { type: Array, default: defaultArray },
);