Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose - multiple schemas using single collection

I'm trying to plan my api, for example:

/animals           // returns all animals
/animals/dogs      // returns all dogs
/animals/cats      // returns all cats
/animals/dogs/:id  // returns dog

So, I have separate models for both 'cat' and 'dog', as they will contain unique properties, however both utilise 'animal' schema as a base plugin.

So my question comes about where to store the data, my options are:

1) Single collection - store both cats and dogs within a single collection, this will make getting data for '/animals' relatively easy with a single query

2) Multiple collections - store both cats and dogs in individual collections, this will make data storage much more logical, however when getting data for '/animals' will require multiple queries and concatenation of that data.

Are there any other options I have missed, or a preferred approach?

Thanks

like image 861
leepowell Avatar asked Nov 13 '22 16:11

leepowell


1 Answers

The options you mentioned are the most appropriate approaches for your problem.

For your specific case if the unique properties of each model isnt that big go with way 1 store all the data in a single collection and query easily.

Otherwise if your models are kinda heavy and have lots of properties separate them into different collections and query them by their respective models.This one seems more work to do but at least will give you more abstraction and flexibility to manipulate heavy models.Meaning go with option 2.

like image 136
Serdar Dogruyol Avatar answered Nov 15 '22 05:11

Serdar Dogruyol