Home >Backend Development >Golang >Add new value to array from another array of same type MongoDB-GO
php Xiaobian Yuzai is here to introduce to you a method of adding new values to an array from another array of the same type, that is, an operation in MongoDB-GO. This method can help developers modify and update arrays more conveniently and improve development efficiency. Below we will introduce the specific steps and usage techniques of this method in detail. We hope it will be helpful to everyone!
I am currently using the mongodb driver on golang and trying to add a field to a document array from an input array of the same type:
type organization struct { id string `bson:"_id,omitempty" json:"id,omitempty" ` name string `bson:"name,omitempty" json:"name,omitempty" validate:"required"` members []member `bson:"members,omitempty" json:"members,omitempty" validate:"required"` owner string `bson:"owner,omitempty" json:"owner,omitempty" validate:"required"` createdat *time.time `bson:"created_at,omitempty" json:"created_at,omitempty"` updatedat *time.time `bson:"updated_at,omitempty" json:"updated_at,omitempty"` } type member struct { userid string `bson:"user_id" json:"user_id" validate:"required"` permissions []string `bson:"permissions" json:"permissions" validate:"required"` addedby string `bson:"added_by" json:"added_by" validate:"required"` status string `bson:"status" json:"status" validate:"required"` addedat time.time `bson:"added_at" json:"added_at"` }
Specifically, I'm trying to append a value from a member array to a value in the document. This is the function I'm currently using:
func (o *organization) addmembers (organizationid string, members []member ) (*organization, error){ // if it doesn't exists it will be created collection := client.database("app-data").collection("organizations") log.println("[app-data-db:add-members] requested new add members: ", members, organizationid) change := bson.m{ "$push": bson.m { "members": bson.m{"$each": members}, }, } id, err := primitive.objectidfromhex(organizationid) if err != nil{ log.println("[app-data-db:add-members] cannot convert to primitive: ", id, organizationid) return nil, err } filter := bson.d{{"_id", id}} result, err := collection.updateone(context.todo(), filter, change) if err != nil { return nil, err } // get newly updated result updatedorg, _ := o.getone(result.upsertedid.(primitive.objectid).hex()) return updatedorg, nil }
From the logs I checked that the id and input member arrays are in the correct format. But I got this error:
http: panic serving 172.23.0.4:42426: interface conversion: interface {} is nil, not primitive.ObjectID
I guess the error is in the way I define the update operation, but I haven't been able to find a solution.
Any help would be greatly appreciated, thanks in advance!
Looks like the error comes from upsertedid
in the *mongo.updateresult
type. For some reason the returned id is zero.
Change
// get newly updated result updatedorg, _ := o.getone(result.upsertedid.(primitive.objectid).hex())
for
// get newly updated result updatedOrg, _ := o.GetOne(organizationID)
Successful.
The above is the detailed content of Add new value to array from another array of same type MongoDB-GO. For more information, please follow other related articles on the PHP Chinese website!