如何连接我的 mongoDB schema/models?

How do I connect my mongoDB schema/models?

我是新手,正在尝试设置我的 noSQL 数据库模型,但遇到困难。目的是“场地”可以创建事件(与场地相关),“艺术家”可以匹配并随后计划事件。如果您是艺术家,您还可以查看仪表板并查看您玩过的活动,所以我需要将艺术家连接到 Venues/events,但不知道如何连接。

下面是我的 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);

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

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);