How do I connect my mongoDB schema/models?

I'm new and trying to set up my noSQL DB model and am struggling. The intention is that "venues" can create events (tied to the venue), and "artists" can match-to and subsequently plan events. If you're an artist, you could also look at your dashboard and see the events you've played, so I need to connect Artists to Venues/events but don't know how.

Below is my Venue model. It works fine in my app, but where do I add Artists in?

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

Below is my Artist model. I haven't tested this one but I think it will work okay.

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']
    },
})

Other than Artist and Venue, I'd like "events" to contain attributes "time" and "date". However, I have no idea where to fit events into the model.. How do I connect "events" between the two models?

Answers 1

  • I would design it like this

    Venue schema (Same as yours): Where all the venues can be maintained independently of events and artists.

    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 schema (same as yours): Where all the artists can be maintained independently of events and venues.

    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 schema: This is where artists and venue come together. Since there will be continuous operations on events (like updating progress) it can be done independently of artists and venues.

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

Related Articles