猫鼬模型,字符串数组,对象数组结构 [英] mongoose model, array of strings, array of objects structure
问题描述
我正在尝试设计我的数据库模型,但我不知道如何拟合 strings
的 array
和 objects 的
array
进去.我目前的模型是:
const mongoose = require("mongoose");const Schema = mongoose.Schema;const 架构 = 新架构({email: { type: String, unique: true, required: true },哈希:{ 类型:字符串,必需:真},createdDate: { type: Date, default: Date.now },设置:{收藏夹:{/* ???*/},购物车:{/* ???*/},状态: {收藏夹:{ 类型:布尔值,默认值:真},搜索:{ 类型:布尔值,默认值:false },类别:{ 类型:Schema.Types.Mixed,默认值:false }}}});schema.set("toJSON", { virtuals: true });module.exports = mongoose.model("用户", 模式);
favorites
数据结构为 ['234', '564', '213', '782']
cart
示例数据结构是:
如何将其作为配置添加到 mongoose
模型中?
Favorites must be an array of String like this: favorites: [String]
对于购物车数组,我们有两个主要选项:
- 我们可以将购物车定义为
子文档
的数组.
const schema = new Schema({email: { type: String, unique: true, required: true },哈希:{ 类型:字符串,必需:真},createdDate: { type: Date, default: Date.now },设置:{收藏夹:[字符串],购物车: [{数量:数量,市场 ID:字符串}],状态: {收藏夹:{ 类型:布尔值,默认值:真},搜索:{ 类型:布尔值,默认值:false },类别:{ 类型:Schema.Types.Mixed,默认值:false }}}});
- 或者我们可以将cart声明为一个
模式类型
的数组.
const schema = new Schema({email: { type: String, unique: true, required: true },哈希:{ 类型:字符串,必需:真},createdDate: { type: Date, default: Date.now },设置:{收藏夹:[字符串],购物车: [新架构({数量:数量,市场 ID:字符串})],状态: {收藏夹:{ type: Boolean, default: true },搜索:{ 类型:布尔值,默认值:false },类别:{ 类型:Schema.Types.Mixed,默认值:false }}}});
对于他们两个,当你创建一个文档时,它会是这样的,注意猫鼬在卡片项目中添加了_id字段.
<代码>{设置":{状态": {收藏夹":真的,搜索":假,类别":假},收藏夹":["234","564","213",782"],购物车":[{"_id": "5e6cd0bd53feb32d50699b79",数量":5,marketId":234"},{"_id": "5e6cd0bd53feb32d50699b78",数量":2,marketId":564"},{"_id": "5e6cd0bd53feb32d50699b77",数量":7,市场ID":213"},{"_id": "5e6cd0bd53feb32d50699b76",数量":3,市场ID":782"}]},"_id": "5e6cd0bd53feb32d50699b75","email": "abc@def.net","hash": "hash...","createdDate": "2020-03-14T12:40:29.969Z","__v": 0,"id": "5e6cd0bd53feb32d50699b75"}
如果您不想在购物车数组中使用 _id
字段,您可以像这样添加 _id: false
选项购物车架构:
购物车:[新架构({数量:数量,市场 ID:字符串},{_id:假})],
以下是一些有用的文档:
I am trying to design my database model and i dont have clue how to fit array
of strings
and array
of objects
into it. My current model is:
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
data structure is ['234', '564', '213', '782']
cart
example data structure is:
[
{ quantity: 5, marketId: '234' },
{ quantity: 2, marketId: '564' },
{ quantity: 7, marketId: '213' },
{ quantity: 3, marketId: '782' }
]
How can i add this as configuration to the mongoose
model?
Favorites must be an array of String like this: favorites: [String]
For the cart array we have two main options:
- We can define the cart as an array of
subdocuments
.
const schema = new Schema({
email: { type: String, unique: true, required: true },
hash: { type: String, required: true },
createdDate: { type: Date, default: Date.now },
settings: {
favorites: [String],
cart: [
{
quantity: Number,
marketId: String
}
],
states: {
favorites: { type: Boolean, default: true },
search: { type: Boolean, default: false },
category: { type: Schema.Types.Mixed, default: false }
}
}
});
- Or we can declare cart as an array of
schema types
.
const schema = new Schema({
email: { type: String, unique: true, required: true },
hash: { type: String, required: true },
createdDate: { type: Date, default: Date.now },
settings: {
favorites: [String],
cart: [
new Schema({
quantity: Number,
marketId: String
})
],
states: {
favorites: { type: Boolean, default: true },
search: { type: Boolean, default: false },
category: { type: Schema.Types.Mixed, default: false }
}
}
});
For both of them, when you create a document, it will look like this, note that mongoose added _id field in the card items.
{
"settings": {
"states": {
"favorites": true,
"search": false,
"category": false
},
"favorites": [
"234",
"564",
"213",
"782"
],
"cart": [
{
"_id": "5e6cd0bd53feb32d50699b79",
"quantity": 5,
"marketId": "234"
},
{
"_id": "5e6cd0bd53feb32d50699b78",
"quantity": 2,
"marketId": "564"
},
{
"_id": "5e6cd0bd53feb32d50699b77",
"quantity": 7,
"marketId": "213"
},
{
"_id": "5e6cd0bd53feb32d50699b76",
"quantity": 3,
"marketId": "782"
}
]
},
"_id": "5e6cd0bd53feb32d50699b75",
"email": "abc@def.net",
"hash": "hash...",
"createdDate": "2020-03-14T12:40:29.969Z",
"__v": 0,
"id": "5e6cd0bd53feb32d50699b75"
}
If you don't want _id
fields in cart array, you can add _id: false
option the cart schema like this:
cart: [
new Schema(
{
quantity: Number,
marketId: String
},
{ _id: false }
)
],
Here are some useful docs:
这篇关于猫鼬模型,字符串数组,对象数组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!