我将创建一个本机应用程序。现在我不确定,在集合中构造数据的最佳方法是什么。
以下情况我有: 我有两个集合 companies 和 users 。两个收藏都有会员合同。那么存储数据的最佳方法是什么?
方法1:
{
users: [
"id": 1,
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
"subcollection_company": [
{
"id": 1,
"name": "Company LLC",
"membership_status": "REQUESTD",
}
],
],
companies: [
"id": 1,
"name": "Company LLC",
"subcollection_users": [
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"membership_status": "REQUESTED",
}
],
],
}
方法2(用数组代替子集合):
{
users: [
"id": 1,
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
"array_company": [
{
"id": 1,
"name": "Company LLC",
"membership_status": "REQUESTED",
}
],
],
companies: [
"id": 1,
"name": "Company LLC",
"array_users": [
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"membership_status": "REQUESTED",
}
],
],
}
方法3(类似sql的旧方法):
{
users: [
"id": 1,
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
],
companies: [
"id": 1,
"name": "Company LLC",
],
user_comany: [
{
userref: 'users/1',
companyref: 'companies/1',
"membership_status": "REQUESTD",
}
]
}
答案 0 :(得分:1)
我认为方法1是最好的方法。
方法2对于大型数据集将失败。假设一家公司拥有数十万用户,那么单个文档的16mb限制将被破坏,您将无法向其添加更多用户。
MongoDB并没有针对连接进行优化,在方法3中,您需要在两个级别上进行连接以获取信息。