【发布时间】:2020-03-14 21:22:03
【问题描述】:
我正在尝试设计我的数据库模型,但我不知道如何将 array 的 strings 和 array 的 objects 放入其中。我目前的模型是:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const schema = new Schema({
email: { type: String, unique: true, required: true },
hash: { type: String, required: true },
createdDate: { type: Date, default: Date.now },
settings: {
favorites: { /* ??? */ },
cart: { /* ??? */ },
states: {
favorites: { type: Boolean, default: true },
search: { type: Boolean, default: false },
category: { type: Schema.Types.Mixed, default: false }
}
}
});
schema.set("toJSON", { virtuals: true });
module.exports = mongoose.model("User", schema);
favorites数据结构为['234', '564', '213', '782']
cart示例数据结构为:
[
{ quantity: 5, marketId: '234' },
{ quantity: 2, marketId: '564' },
{ quantity: 7, marketId: '213' },
{ quantity: 3, marketId: '782' }
]
如何将此作为配置添加到mongoose 模型?
【问题讨论】:
标签: javascript node.js mongodb mongoose