我尝试创建包含对象数组的mongoose模式。模式如下:
const mapSchema = new Schema({
name: {
type: String
},
description: {
type: String
},
rate: {
type: Number,
default: 0
},
sumOfRates: {
type: Number,
default: 0
},
places: [
{
name: String,
description: String,
difficulty: { type: String, default: 0 },
sumOfRates: { type: String, default: 0 },
lat: Number,
lng: Number
}
]
}, {
timestamps: true
})
Places是一个对象数组。我尝试创建mongoose模式,并将其用作places数组的元素(places:[ref:'placeSchema'])。
const placeSchema = new Schema({
name: {
type: String
},
description: {
type: String
},
difficulty: {
type: Number,
default: 0
},
lat: {
type: Number
},
lng: {
type: Number
}
})
但我认为它会在mongo中创建另一个分离的文档(places文档)。是这样吗?我试图避免这种情况,将其保留在一个文档中。现在(上面贴的代码)在postman中尝试插入json时会出错:
{
"name": "ergergergg",
"description": "rgergrg",
"places": [
{
"name":"cvcv",
"description":"rgergrg",
"lat":233,
"lng":232
}]
}
错误:
ValidationError: places: Cast to Array failed for value "[object Object]" at path "places"
为什么会出现这样的错误?如何修复呢?