Skip to content

Instantly share code, notes, and snippets.

@vincentntang
Last active April 7, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincentntang/26f8a5d505e633a9dc8ac7b57f144da8 to your computer and use it in GitHub Desktop.
Save vincentntang/26f8a5d505e633a9dc8ac7b57f144da8 to your computer and use it in GitHub Desktop.
MongoDB Slack : POST vs AUTHORS one-to-many
// A post can have multiple authors
// An author can have many posts
// many-to-many relationship
{
Post: [
postID: 'postID',
postTitle: 'How to build a website'
postContent: '<div><p>HTML or markdown content here<p></div>'
authorID: ['AuthorID1','AuthorID2'] // We define many-to-many relationship here
],
Author: [
authorID: 'authorID',
authorName: 'John Doe'
authorTitle: 'Web Developer'
// postID: [PostID1, PostID2] // this is not necessary because we define it in `Post`
]
}
// A post has one author
// an author can have many posts
// one-to-many relationship (author : post)
{
Post: [
postID: 'postID',
postTitle: 'How to build a website'
postContent: '<div><p>HTML or markdown content here<p></div>'
authorID: 'authorID' // this defines foreign key for one-to-many or even one-to-one relationship
],
Author: [
authorID: 'authorID',
authorName: 'John Doe'
authorTitle: 'Web Developer'
// postID: [PostID1, PostID2] // this is not necessary because we define it in `Post`
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment