I am trying to update the embedded field “Comments” in MongoDB. Below is the code.
- Struct.
package entities
type IMDBRegistry struct {
MovieName string `json:"moviename,omitempty"`
Rating string `json:"rating,omitempty"`
RatingCount int `json:"peoplecount,omitempty"`
Comments map[string]interface{} `json:"comments,omitempty"`
}
- Wrapper written on MongoDB for Update operation.
func UpdateDocument(filterObject interface{}, operation string, update map[string]interface{}) (int64, error) {
mongoObj := connect.GetMongoObject()
if mongoObj == nil {
log.Fatalln(constants.ERR_DB)
return 0, nil // TODO
}
collection := mongoObj.Database(constants.DB_NAME).Collection(constants.COLLECTION_NAME_USER)
jsonData, err := json.Marshal(filterObject)
if err != nil {
log.Println("Error while Marshalling in UpdateDocument")
return 0, err
}
var m interface{}
json.Unmarshal(jsonData, &m)
filter := m.(map[string]interface{})
jsonDataUpdate, err := json.Marshal(update)
if err != nil {
return 0, err
}
var m1 interface{}
json.Unmarshal(jsonDataUpdate, &m1)
updateString := bson.M{operation: update}
result, err := collection.UpdateOne(context.TODO(), filter, updateString)
log.Println("Result is ::: ", result)
if err != nil {
return 0, err
} else {
log.Println("Returning from here.")
return result.ModifiedCount, nil
}
}
- Code where I am trying to update the “Comments” field , I have tried various operators not just one.
Overview: I am reading the moviename which is provided for instance moviename exist then on the basis of it I am fetching whole record then storing comments in temporary variable then appending new comment with old ones then trying to save.
func AddComment(params add_comment.PostcommentsParams) middleware.Responder {
log.Println("Processing request to AddComment.")
userName := params.Body.UserName
movieName := params.Body.MovieName
movieComment := make(map[string]interface{}, 0)
movieComment["comments"] = params.Body.MovieComment
if UserNameIsValid(userName) == userName && len(userName) > 0 {
//Checking if provided moviename exist in DB.
if MovieNameIsValid(movieName) == movieName && len(movieName) > 0 && len(movieComment) > 0 {
searchResult, err := ReadDocument(entities.IMDBRegistry{Comments: movieComment}, &entities.IMDBRegistry{})
if err != nil {
}
if searchResult != nil {
result := searchResult.(entities.IMDBRegistry)
commentUp := result.Comments
_, err := UpdateDocument(entities.IMDBRegistry{Comments: result.Comments}, "$push", nil)
if err != nil {
}
}
//UpdateDocument(entities.IMDBRegistry{Comments: movieComment})
} else { // If moviename is invalid.
errMsg := constants.INVALID_MOVIENAME + constants.REQUEST_FAILED
return add_comment.NewPostcommentsInternalServerError().WithPayload(&models.Error{Code: constants.INTERNAL_ERROR_CODE, Message: &errMsg})
}
}
//Return error if above conditions are not satisfied.
errMsg := constants.INVALID_USER + constants.REQUEST_FAILED
return add_comment.NewPostcommentsInternalServerError().WithPayload(&models.Error{Code: constants.INTERNAL_ERROR_CODE, Message: &errMsg})
}
If any other information is needed I will provide . Any help is appreciated .