1

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.

1 Answer 1

1

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
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for the quick response. This looks just right, I'll be using this model now. Thanks again :))
You should then mark this answer as the correct one, show some love back :)
Right sorry I'm new to stackoverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.