3

How to manage relationship in go-mongo?

I am trying as follows but not working:

type Post struct {
    ID         primitive.ObjectID `json:"id" bson:"_id"`
    Text       string             `json:"text" bson:"text,omitempty"`
    Comments   []Comment          `json:"comments" bson:"comment,omitempty"`
}

type Comment struct {
    ID         primitive.ObjectID `json:"id" bson:"_id"`
    Text       string             `json:"text" bson:"text,omitempty"`
    PostID     primitive.ObjectID `json:"postId" bson:"post_id,omitempty"`
}

JSON Response output now has null comments:

{
    "id": "639c74bcb997f791b691bfa9",
    "text": "my post text here",
    "comments": null
}

JSON expected output I want:

{
    "id": "639c74bcb997f791b691bfa9",
    "text": "my post text here",
    "comments": [
      {
        "id": "100c74bcb997f791b691bfa9",
        "text": "my comment text here"
      },
      {
        "id": "200c74bcb997f791b691bfa9",
        "text": "my comment text here"
      }
    ]
}
its-me-mahmud
  • 690
  • 1
  • 7
  • 14
Zayn Korai
  • 493
  • 1
  • 6
  • 24

0 Answers0