How to design MongoDB document for the hierarchical user flow?

50 views Asked by At

Let us assume the following as the user flow in a multi tenant application with each tenant having its own set of catalogs or share a common catalog.

User logs into the application and see

UI with list of catalogs - User clicks on one catalog and navigates to

UI with list of categories - User clicks on one category and navigates to

UI with list of Groups - User clicks on one group and navigates to

UI with list of Articles - User clicks on one article and navigates to

UI all the details for an Article - an article can be a doc, video or pdf and other metadata

I am new to MondoDB and based on my understanding the document structure could be

    [
        {
          "TenantName": "Tenant A",
          "catalogs": [
            {
              "Categories": [
                {
                  "Groups": [
                    {
                      "Articles": [
                        {
                          "Article Title": "Title 1"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "TenantName": "Tenant B",
          "catalogs": [
            {
              "Categories": [
                {
                  "Groups": [
                    {
                      "Articles": [
                        {
                          "Article Title": "Title 1"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
    ]

Is the above design the right approach to start with?

But is this performance efficient as I read the root document is performance efficient but the nested may not be. Since the flow of the application is step by step I am worried whether this approach will be helpful considering the performance and the storage.

PS: The category can be part of multiple tenants, same groups can be part of multiple categories, same article can be part of multiple groups

1

There are 1 answers

0
hiren207 On

This is how you can use it with mongoose (ORM)

// Tenant Model
const TenantSchema = new mongoose.Schema({
  TenantName: {
    type: String,
    required: true,
  },
  catalogs: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Catalog',
  }],
});

const Tenant = mongoose.model('Tenant', TenantSchema);


// Catalog Model
const CatalogSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  tenant: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Tenant',
  },
  categories: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Category',
  }],
});

const Catalog = mongoose.model('Catalog', CatalogSchema);


// Category Model
const CategorySchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  catalog: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Catalog',
  },
  groups: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Group',
  }],
});

const Category = mongoose.model('Category', CategorySchema);


// Group Model
const GroupSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  category: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Category',
  },
  articles: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Article',
  }],
});

const Group = mongoose.model('Group', GroupSchema);


// Article Model
const ArticleSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },
  group: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Group',
  },
  type: {
    type: String,
    enum: ['document', 'video', 'pdf'],
    required: true,
  },
  metadata: {
    // Add any additional metadata fields here
  },
});

const Article = mongoose.model('Article', ArticleSchema);