MongoDB 共享用户数据最佳实践

问题描述 投票:0回答:0

使用 MongoDB 作为另一个用户访问嵌套用户数据的最佳方式是什么?

我无法找到任何关于能够从其他用户访问用户数据的最佳实践的信息。

鉴于 MongoDB 是基于文档的存储,我认为这是存储与用户耦合的数据的最佳方式。我的问题是我不确定允许用户 2 和 3 访问用户 1 拥有的列表的最佳方法是什么。

{
  "_id": 1,
  "username": "johndoe",
  "hashed_password": "xxxxx",
  "lists": [
    {
      "id": 1,
      "name": "list 1",
      "owner_id": 1,
      "shared": true,
      "shared_users": [2, 3],
      "items": [
        {
          "name": "item 1",
          "description": "item description"
        },
        {
          "name": "item 2",
          "description": "item description"
        }
      ]
    },
    {
      "id": 1,
      "name": "different list",
      "owner_id": 1,
      "shared": false,
      "shared_users": [],
      "items": [
        {
          "name": "item 1",
          "description": "item description"
        },
        {
          "name": "item 2",
          "description": "item description"
        }
      ]
    }
  ]
}

我在想,将用户和列表分开是解决这个问题的最佳方法,因为这样每个用户都可以作为所有者或共享查看者引用每个列表。但我知道这不是一个理想的解决方案。

{
    "_id": 1,
    "type": "USER",
    "username": "johndoe",
    "hashed_password": "xxxxx",
    "owned_lists": [{ "mapped_id": 5 }]
},
{
    "_id": 5,
    "type": "LIST",
    "name": "list 1",
    "owner_id": 1,
    "shared": true,
    "shared_users": [2, 3],
    "items": [
        {
            "name": "item 1",
            "description": "item description"
        },
        {
            "name": "item 2",
            "description": "item description"
        }
    ]
}

我发现this问题从应用程序使用(读/写)的角度考虑了一些注意事项,但对访问嵌套用户数据的影响不大。

然后我应该为列表和用户设置 2 个集合,还是将它们全部放在同一个集合中?

实现这一目标的最佳方法是什么?

database mongodb database-design
© www.soinside.com 2019 - 2024. All rights reserved.