我尝试创建包含对象数组的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
})
位置是对象数组。我尝试创建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文档)中创建另一个separeted文档。可以?我试图避免这种情况将其保存在一个文档中。现在(上面粘贴的代码),给我一个错误,当我尝试在邮递员中插入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"
为什么会有这样的错误?如何解决这个问题?
答案 0 :(得分:1)
适当的方法是使用子模式。
const subSchema = new Schema({
name: {
type: String,
},
description: {
type: String,
},
difficulty: {
type: Number,
default: 0,
},
lat: {
type: Number,
},
lng: {
type: Number,
},
});
const mapSchema = new Schema({
name: {
type: String,
},
description: {
type: String,
},
rate: {
type: Number,
default: 0,
},
sumOfRates: {
type: Number,
default: 0,
},
places: [subSchema],
}, {
timestamps: true
});
插入新数据
mapSchema.insert({
name: "ergergergg",
description: "rgergrg",
places: [
{
name: "cvcv",
description": "rgergrg",
lat: 233,
lng: 232,
},
],
});
答案 1 :(得分:1)
要使 subSchema 可选,您可以这样做@DanMossa :
function onEditAdded(e) {
var activeSheet = e.source.getActiveSheet();
if (activeSheet.getName() == "New Hires") {
var aCell = e.source.getActiveCell(), col = aCell.getColumn();
if (col == 20) {
var dateCell = aCell.offset(0,1);
if (aCell.getValue() === true) {
var email = Session.getActiveUser().getEmail();
Logger.log(email);
dateCell.setValue(email);
} else {
dateCell.setValue("");
}
}
}
}