How would I model the following data?

Hello,

Is it possible to model this tree of data, so that is searching but within a single document?

image

I thought about, just creating an array, and putting fiction, science fiction, general, miliatry in each element, but then I lose the hierarchical nature of the data.

Then I considered and array, but with the following structure (forgetting to include quotes of course): -

Subjects:
[{ level1: Fiction, Level2: Science Fiction, Level3: General},
 { level1: Fiction, Level2: Science Fiction, Level3: Action & Adventure},
 { level1: Fiction, Level2: Science Fiction, Level3: Military}]

Indexing on Subjects, level1, level2, level3, level4 etc?

Or should it be in a separate document, though it seems it should be in the main book document Or maybe both versions?

Thank you.

There is a guide about tree structures on Mongo’s website. https://docs.mongodb.com/manual/applications/data-models-tree-structures/

I think for your use case I would use Materialized Paths as it seems suitable and easy to implement. Using an array as you intend is a lot harder to query.

2 Likes

Thank you.

Another valuable link to add to ever expanding bookmarks :slight_smile:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.