在 MongoDB 中存储文档的正确方法

发布于 2025-01-14 16:41:22 字数 507 浏览 3 评论 0 原文

我很长时间以来一直对 MongoDB 有同样的问题。因此,我尝试为邀请我的不和谐机器人加入其公会的用户存储一些用户数据。它根据行会 ID 和用户 ID 存储它们,因为一个用户可能位于具有同一机器人的多个服务器中。我想知道为每个行会制作一份文档并将每个用户添加到该文档中是否正确,或者我应该为每个用户制作一份带有行会 ID 密钥和用户 ID 密钥的单独文档。

例如,我在这篇文章中附加了两张照片,第一张照片的特点是一个文档,其中“用户”字段嵌套在一个对象中,该对象的键作为用于查询的行会 ID。第二个功能是为每个用户提供一个单独的文档,并具有用于查询的单独的“用户 ID”和“行会 ID”密钥。

单个文档文件

多文档文件

感谢任何和所有帮助。先感谢您。

I've been having this same question about MongoDB for the longest time. So I'm trying to store some user data for users who invite my discord bot to their guild. It stores them based off guild ID and user ID, because one user could be in multiple servers with the same bot. I was wondering if it would be correct to make one document for each guild and add every user to that one document, or should I make a separate document for each user with a guild ID key and user ID key.

For example I've attached 2 photos to this post, the first features one document with a "Users" field nested into an object with the key as the guild ID used for querying. The second features a separate document for each user with a separate "User ID" and "Guild ID" key used for querying.

Single Document File

Multi Document File

Any and all help is appreciated. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2025-01-21 16:41:22

这是一个你可以做的想法。这样您就可以以任何您喜欢的方式获取数据并聚合,并且不会有任何不必要的嵌套。简单灵活

// Guild Schema

{
  guildId: Number,
  guildName: String,
  other: String
}

// User Schema

{
  userId: Number,
  guild: {
    type: Schema.Types.ObjectId,
    ref: "Guild"
  },
  userInfo:{
    name: String,
    address: String,
    other: String
  }
}

This is an idea what you can do. This way you can fetch data and aggregate any way you like, and you won't be having any unnecessary nesting. Simple and flexible

// Guild Schema

{
  guildId: Number,
  guildName: String,
  other: String
}

// User Schema

{
  userId: Number,
  guild: {
    type: Schema.Types.ObjectId,
    ref: "Guild"
  },
  userInfo:{
    name: String,
    address: String,
    other: String
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文