node.js - 对象作为属性 mongoose mongodb
问题描述
我需要一个猫鼬模型中的属性,如下所示:
customRanks应该有不同的键和每个键的不同值
const config = new Schema({
id: { type: String, required: true, unique: true },
//...other property
customRanks: Object,
});
解决方案
custome rank schema
如果您想以特定格式保存customRanks字段的数据,您可以定义并使用它作为对象
const customRanksSchema = new Schema({
key1: { type: Number, default: 0 },
key2: { type: Number, default: 0 },
key3: { type: String, default: '' }
})
如果您想要静态架构并为您的客户排名对象提供特定字段
// if you want to save customeRanks as array of object
customRanks: [{ type: customRanksSchema }],
// if you want to save custom rank as a object
customRanks: customRanksSchema
如果您正在寻找没有密钥的验证。然后,您可能需要在使用 javascript插入/创建/更新之前验证数据。
推荐阅读
- java - api 29 的源代码没有下载?
- python - 您如何处理 REST API 服务器中的高吞吐量函数?
- java - 如何通过 DataAccessException 在 postgres 数据库上检索 SQL 错误代码?
- php - document.getElementById() 作为 PHP SQL 查询参数
- c# - 地图路线将空白空间转换为 -
- c++ - 在 sniffer.h libtins 上获取 VS C2664
- python - Python:在 Windows 操作系统中更改文件/文件夹属性
- flutter - 无法重建 StreamProvider 以更新其当前数据
- google-kubernetes-engine - 使用 GKE 工作负载标识时出现 MetadataServerException
- java - 从 Windows 10 平台上的 USB 设备读取 Java usb4java