javascript - 猫鼬 - 如何让子文档符合模式
问题描述
所以我有以下模式:
var Item_Detail = new Schema(
{
content: {
index: true,
type: Array
},
is_private: {
default: false,
index: true,
type: Boolean
},
order: {
index: true,
required: true,
type: Number
},
table: {
default: {},
type: Object
},
title: {
required: true,
index: true,
type: String,
},
type: {
default: "text",
enum: ["text", "table"],
index: true,
type: String
},
},
{
strict: false
}
)
const Item = new Schema(
{
details: {
default: [],
index: true,
type: [Item_Detail],
},
display_name: {
default: "",
index: true,
type: String,
},
image: {
default: "http://via.placeholder.com/700x500/ffffff/000000/?text=No%20Image&",
type: String
},
is_private: {
default: false,
index: true,
type: Boolean
},
tags: {
index: true,
type: [Tag]
}
},
{
strict: false
}
)
现在,Item_Detail
将成为 的子文档Item
,但我不太确定应该如何执行default
s 和type
限制。我也不想Item_Detail
自己成为一个集合,所以使用create
或save
可能不适合。
解决方案
我认为您可以为此使用嵌入式文档,因此在您的项目模式中您可以嵌入item_detail:
const Item = new Schema({
...
item_detail: item_detail
})
然后在服务器上,当您要添加 item_detail 时,您可以执行以下操作
myItem = new Item({//enter item data here})
//assign item detail here
myItem.item_detail = item_detail ;
然后继续保存
myItem.save()
推荐阅读
- angular - 为什么格式化数据不起作用Angular?
- reactjs - 样式化组件和 ReactJs 的 CSS 中的动态属性
- xcode - 出现在故事板选择下拉列表中的资产
- javascript - 在Javascript中制作水平滚动菜单
- spring - 如何在 CDI bean 中调用服务类接口
- android - 如何从firebase db android中的2个表格中检索数据?
- javascript - Reactjs将不同的属性传递给具有相似按钮的相同功能?
- bluetooth - Gatttool 在几秒钟后停止接收通知(不是因为断开连接)
- google-cloud-platform - 云函数中如何获取快照的ID?
- typescript - 字典中的打字稿索引字符串类型,它是实现中键的联合类型