Insert one to many database using mongodb

Here are two models User and Item

type User struct {
    ID        primitive.ObjectID   `json:"id" bson:"_id,omitempty"`
    Email     string        `json:"email,omitempty" bson:"email,omitempty"`
    FirstName string        `json:"firstName,omitempty" bson:"firstName,omitempty"`
    LastName  string        `json:"lastName,omitempty" bson:"lastName,omitempty"`
    Password  string        `json:"password,omitempty" bson:"password,omitempty"`
    Item      Item          `json:"item,omitempty" bson:"item,omitempty"`
}

type Item struct {
    ID          primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
    Category    string             `json:"category,omitempty" bson:"category,omitempty"`
    Name        string             `json:"name,omitempty" bson:"name,omitempty"`
    Description string             `json:"description,omitempty" bson:"description,omitempty"`
    Price       int                `json:"price,omitempty" bson:"price,omitempty"`
}

I have inserted many samples into Item collection.
How do I insert data into user collection by supplying it item details to it.

I am not sure if I understand the question, but one to many indicates a “relation”. Using a relational database (Postgresql, MySQL etc) should be simpler to achieve a relation between tables.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.