In this lab, we're going to build a small-scale music streaming service database using MongoDB and Mongoose. We will create collections that represent artists, albums, songs, and playlists, and establish relationships between them.
By the end of this lab, you'll have a working MongoDB database that models a music streaming service, where artists release albums, albums contain songs, and users can create playlists of their favorite songs.
We will create four collections:
- Artists: Stores information about the artists.
- Albums: Stores details about albums and references the artist.
- Songs: Stores individual songs and references the album they belong to.
- Playlists: Stores user-created playlists that can contain multiple songs from different albums.
- Create a new file called
seed.js
. - Initialize npm:
npm init -y
. - Install the necessary packages:
npm install mongoose dotenv
.
- Create a
.env
file. - Create a
.gitignore
file and add the folders and files you don't want to push to your repository:
node_modules
package-lock.json
.env
- Add your MongoDB connection string in the
.env
file:
MONGODB_URL=mongodb://connection-string-to-your-mongoDB-with-user-and-password/your-database-name
First, connect to your MongoDB database using Mongoose:
Reveal the code π
const mongoose = require("mongoose");
require("dotenv").config();
mongoose
.connect(process.env.MONGODB_URL)
.then((res) => {
console.log("Connected to the database:", res.connections[0].name);
})
.catch((error) => {
console.log("Error connecting to the database", error);
});
Create a model for the artists with a name and genre:
Reveal the code π
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const artistSchema = new Schema({
name: String,
genre: String,
});
const Artist = mongoose.model("Artist", artistSchema);
module.exports = Artist;
Create a model for the albums with a title, release year, and a reference to the artist (by ObjectId):
Reveal the code π
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const albumSchema = new Schema({
title: String,
releaseYear: Number,
artist: { type: Schema.Types.ObjectId, ref: "Artist" },
});
const Album = mongoose.model("Album", albumSchema);
module.exports = Album;
Create a model for the songs with a title, duration, and a reference to the album (by ObjectId):
Reveal the code π
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const songSchema = new Schema({
title: String,
duration: Number,
album: { type: Schema.Types.ObjectId, ref: "Album" },
});
const Song = mongoose.model("Song", songSchema);
module.exports = Song;
Create a model for user playlists with a name and an array of song references (by ObjectId):
Reveal the code π
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const playlistSchema = new Schema({
name: String,
songs: [{ type: Schema.Types.ObjectId, ref: "Song" }],
});
const Playlist = mongoose.model("Playlist", playlistSchema);
module.exports = Playlist;
Now that the models are in place, let's seed the database with some initial data:
- Create an array of artists, albums, songs, and playlists.
- Use the
insertMany
method to add the data to your collections. - You can use chatGPT to generate a JSON file with sample data for artists, albums, songs, and playlists.
Something like this:
{
"artists": [
{
"name": "Artist 1",
"genre": "Pop"
},
{
"name": "Artist 2",
"genre": "Rock"
}
],
"albums": [
{
"title": "Album 1",
"releaseYear": 2021,
"artist": "artist_id"
},
{
"title": "Album 2",
"releaseYear": 2020,
"artist": "artist_id"
}
],
"songs": [
{
"title": "Song 1",
"duration": 180,
"album": "album_id"
},
{
"title": "Song 2",
"duration": 240,
"album": "album_id"
}
],
"playlists": [
{
"name": "My Playlist",
"songs": ["song_id", "song_id"]
}
]
}
Try to make GPT create the proper objectIDs for the artist, album, and song references so they match.
Take your time, this is the most challenging part of the lab. π§
You may need to use the Promise.all method to wait for all the data to be inserted before closing the connection.
Hint:
Promise.all([
Artist.insertMany(data.artists),
Album.insertMany(data.albums),
Song.insertMany(data.songs),
Playlist.insertMany(data.playlists),
]);
Reveal the code π
const Artist = require("./models/Artist.model");
const Album = require("./models/Album.model");
const Song = require("./models/Song.model");
const Playlist = require("./models/Playlist.model");
// import the data
const data = require("./data.json");
mongoose
.connect(process.env.MONGODB_URL)
.then((res) => {
console.log("Connected to the database:", res.connections[0].name);
})
.then(() => {
return Promise.all([
Artist.insertMany(data.artists),
Album.insertMany(data.albums),
Song.insertMany(data.songs),
Playlist.insertMany(data.playlists),
]);
})
.catch((error) => {
console.log("Error connecting to the database", error);
});
Happy coding! π§