如何连接我的mongoDB架构/模型



我是新手,正在尝试建立我的noSQL DB模型,但我很挣扎。其意图是";场馆";可以创建事件(与场地相关(;艺术家";可以匹配并随后计划事件。如果你是一名艺术家,你也可以查看你的仪表板,查看你参加过的活动,所以我需要将艺术家与场馆/活动联系起来,但不知道如何联系。

下面是我的场地模型。它在我的应用程序中运行良好,但我在哪里添加艺术家?

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const VenueSchema = new Schema({
title: String,
image: String,
price: Number,
description: String,
location: String
});
module.exports = mongoose.model('Venue', VenueSchema);

下面是我的艺术家模型。我还没有测试过这个,但我认为它会正常工作。

const mongoose = require('mongoose');
const { Schema } = mongoose;
const artistSchema = newSchema({
name: {
type: String,
required: [true, 'Artist must have a name']
},
genre: {
type: String
},
email: {
type: String,
required: [true, 'Contact email required']
},
})

除了艺术家和场地,我想要";事件";以包含属性";时间";以及";日期";。然而,我不知道在哪里可以将事件放入模型中。。如何连接";事件";在两个模型之间?

我会像这样设计

Venue模式(与您的模式相同(:所有场馆都可以独立于活动和艺术家进行维护。

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const VenueSchema = new Schema({
title: String,
image: String,
price: Number,
description: String,
location: String
});
module.exports = mongoose.model('Venue', VenueSchema);

Artist模式(与您的模式相同(:所有艺术家都可以独立于活动和场地进行维护。

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const artistSchema = newSchema({
name: {
type: String,
required: [true, 'Artist must have a name']
},
genre: {
type: String
},
email: {
type: String,
required: [true, 'Contact email required']
},
})
module.exports = mongoose.model('Artist', artistSchema);

Events模式:这是艺术家和场地聚集在一起的地方。由于活动将持续进行(如更新进度(,因此可以独立于艺术家和场地进行。

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const eventSchema = new Schema({
venue_id: {
type: Schema.Types.ObjectId,
ref: 'Venue',
index: true
},
artist_id: {
type: Schema.Types.ObjectId,
ref: 'Artist',
index: true
},
created: {
type: Date,  // Captures both date and time
default: Date.now
}
});
module.exports = mongoose.model('Event', eventSchema);

最新更新