After completing the schemas the database logic can be incorporated to interact with MongoDB:
- Open the file cataloguedb.js in the data folder. Import the Artist, Album, and Song schemas:
var Artist = require("../model/artist.js"); var Album = require("../model/album.js"); var Song = require("../model/song.js");
- Next, the getArtist function is coded and exported to be used by the Controller logic. The code is shown in the following code block:
module.exports.getArtist = function (request, response) { var query = Artist.find({"artist_id" : request}, {'_id' : 0}).populate({ "path": "albums", "model": "Album", "populate": { path: 'songs', model: "Song" } }) query.exec(function (err, artist) ...