如何为分层用户流程设计MongoDB文档?

huangapple go评论42阅读模式
英文:

How to design MongoDB document for the hierarchical user flow?

问题

以下是您要翻译的内容:

假设以下内容是多租户应用程序中的用户流程,每个租户都有自己的目录集或共享一个公共目录。

用户登录应用程序并看到

UI 上有目录列表 - 用户点击一个目录然后导航到

UI 上有类别列表 - 用户点击一个类别然后导航到

UI 上有群组列表 - 用户点击一个群组然后导航到

UI 上有文章列表 - 用户点击一篇文章然后导航到

UI 上有文章的所有细节 - 一篇文章可以是文档、视频或PDF以及其他元数据

我对MondoDB还不熟悉,根据我的理解,文档结构可能是

[
{
"租户名称": "租户A",
"目录": [
{
"类别": [
{
"群组": [
{
"文章": [
{
"文章标题": "标题1"
}
]
}
]
}
]
}
]
},
{
"租户名称": "租户B",
"目录": [
{
"类别": [
{
"群组": [
{
"文章": [
{
"文章标题": "标题1"
}
]
}
]
}
]
}
]
}
]

上述设计是否是开始的正确方法?

但是,从性能的角度来看,这种方法是否高效?因为应用程序的流程是逐步进行的,我担心这种方法是否有助于性能和存储,因为根文档性能高效,但嵌套性能可能不高。同一类别可以属于多个租户,同一群组可以属于多个类别,同一文章可以属于多个群组。

英文:

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

得分: 1

这是如何在 mongoose (ORM) 中使用它的方式:

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

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

// 目录模型
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);

// 类别模型
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);

// 组模型
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);

// 文章模型
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: {
    // 在此处添加任何额外的元数据字段
  },
});

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

希望这有帮助。

英文:

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);

huangapple
  • 本文由 发表于 2023年6月12日 13:53:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定