Step 6 – code the interaction with the database

After completing the schemas the database logic can be incorporated to interact with MongoDB:

  1. 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");
  1. 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) ...

Get Implementing Oracle API Platform Cloud Service now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.