Node.js 如何使用mongodb(mongoose模式)创建模式?
我有型号和价格的手机列表。我想为此创建一个模式。我对猫鼬和猫鼬是个新手。如果有人帮我,我已经添加了我的要求Node.js 如何使用mongodb(mongoose模式)创建模式?,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,我有型号和价格的手机列表。我想为此创建一个模式。我对猫鼬和猫鼬是个新手。如果有人帮我,我已经添加了我的要求 Categories : nokia sub Categories :Nokia Lumia 730 -7,000, Nokia 225 -5,000, Nokia Lumia 1020 -6,000, Nokia Lumia 530 -8,0000 Samsung Galaxy A7:
Categories :
nokia
sub Categories :Nokia Lumia 730 -7,000,
Nokia 225 -5,000,
Nokia Lumia 1020 -6,000,
Nokia Lumia 530 -8,0000
Samsung Galaxy A7:
Samsung Galaxy A7 -10,000,
Samsung Galaxy A3 -12,000,
Samsung Galaxy One5 -5,000,
Samsung Galaxy S5 Neo -6,000
HTC One M9s:
HTC One M9s -9,000,
HTC Desire 728G -12,000,
HTC Desire 526 -4,000,
我的期望:
我如何设计模式来解决以下情况
var ShopSchema = new Schema({
Email: {
type: String,
default: '',
trim: true,
},
Storename: {
type: String,
default: '',
trim: true
},
Type: {
type: String,
default: '',
trim: true
},
Categories: {
type: String,
default: '',
trim: true
}
});
您可以为商店和类别创建两个不同的集合 并以嵌套方式生成模式
var Categories = new Schema({
name : String
, subcategories : {
name : String,
model : String
}
});
var ShopSchema = new Schema({
Email: {
type: String,
default: '',
trim: true,
},
Storename: {
type: String,
default: '',
trim: true
},
Type: {
type: String,
default: '',
trim: true
},
Categories : [Categories]
});
要了解mongoose中的嵌套模式,可以访问
我举一个例子来了解更多-
首先,我们必须保存店铺名称
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : []
}
当我们保存单个类别时,它将同时进入集合类别和存储
商店收藏-
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : [
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
]
}
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
类别收集-
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : [
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
]
}
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
现在,如果我们想插入子类别,它将作为子文档进入两个集合
类别集合如下所示-
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : [
{
"name" : "Nokia Lumia",
"model":"Nokia Lumia 730 -7,000"
}
]
}
商店收藏将如下所示-
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : [
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : [{
"name" : "Nokia Lumia",
"model":"Nokia Lumia 730 -7,000"
}
]
}
]
}
您还可以根据需要更改模式
谢谢您可以为店铺和类别制作两种不同的收藏 并以嵌套方式生成模式
var Categories = new Schema({
name : String
, subcategories : {
name : String,
model : String
}
});
var ShopSchema = new Schema({
Email: {
type: String,
default: '',
trim: true,
},
Storename: {
type: String,
default: '',
trim: true
},
Type: {
type: String,
default: '',
trim: true
},
Categories : [Categories]
});
要了解mongoose中的嵌套模式,可以访问
我举一个例子来了解更多-
首先,我们必须保存店铺名称
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : []
}
当我们保存单个类别时,它将同时进入集合类别和存储
商店收藏-
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : [
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
]
}
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
类别收集-
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : [
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
]
}
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : []
}
现在,如果我们想插入子类别,它将作为子文档进入两个集合
类别集合如下所示-
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : [
{
"name" : "Nokia Lumia",
"model":"Nokia Lumia 730 -7,000"
}
]
}
商店收藏将如下所示-
{
"_id" : ObjectId("567a8f004cb6178545bac89c"),
"Email" : "dineshaws@hotmail.com",
"Storename" : "Dineshaws",
"Type" : "XYZ",
"Categories" : [
{
"_id" : ObjectId("567a9be04cb6178545bac89d"),
"name" : "nokia",
"subcategories" : [{
"name" : "Nokia Lumia",
"model":"Nokia Lumia 730 -7,000"
}
]
}
]
}
您还可以根据需要更改模式
谢谢如果我想在中添加更多数据,请使用我的给定数据添加一些示例subcategories@komal,我很乐意帮助你。谢谢,我需要你的帮助,先生@Dineshaws@komal是的,让我知道你的问题?如果我想添加更多的数据,你可以添加一些例子使用我的给定数据吗subcategories@komal,我很乐意帮助你。谢谢,我需要你的帮助,先生@Dineshaws@komal是的,让我知道你的问题?