Hello Divjot, nice to meet you again. I hope you are doing well and are safe.
//The code here is the API that will access db by id in string
func FindAndUpdateDocByStringID(coll *mongo.Collection, id string, update bson.M, result interface{}) error {
prefix := coll.Name()
err := coll.FindOneAndUpdate(context.TODO(), bson.D{{Key: "_id", Value: id}}, update).Decode(result)
if err != nil {
if strings.Contains(err.Error(), NoDocument) {
return errors.NotFound(prefix+"_not_found", err.Error())
}
return errors.InternalServerError(prefix+"_internal_error", err.Error())
}
return nil
}
//The code here is the calling of the API
update := bson.M{"$inc": bson.M{"nbr_details_views": 1}}
modelPlace := Place{}
err := dboperate.FindAndUpdateDocByStringID(collection, in.Value, update, &modelPlace)
//The code here is the definition of the Place
type Place struct {
// Id of the Place
ID string `json:"id,omitempty" bson:"_id,omitempty"`
// UserID of the owner of the Place
UserID primitive.ObjectID `json:"user_id,omitempty" bson:"user_id,omitempty"`
// City of the Place
City string `json:"city,omitempty" bson:"city,omitempty"`
// Description of the Place (long text)
Description string `json:"description,omitempty" bson:"description,omitempty"`
// Photos lists the available photos URL
Photos []string `json:"photos,omitempty" bson:"photos,omitempty"`
// Nbr of views on detail this place
NbrDetailsViews int32 `json:"nbr_details_views,omitempty" bson:"nbr_details_views,omitempty"`
}