0

I have an artist model that has a collection of songs. I am not populating the related songs in the blueprints config, but I would like to at least get a count back so I know how many songs are on each artist. Here is what my simple artist model looks like:

module.exports = {

attributes: { name:{ type: 'string', required: true }, songs:{ collection: 'song', via: 'artist' }
} };

matt
  • 103
  • 1
  • 7

1 Answers1

1

In order to get a count back you will have two options.

Option A.) Run a second query

Song.count({artist:artist}).exec(/*...*/)

Option B.) Keep a count value in the artist model that increases / decreases as songs are added / removed. This is probably your best option as it will request the least stress on the DB in those cases where artists are being scanned / queried more than songs are being added.

Meeker
  • 5,979
  • 2
  • 20
  • 38