Commit f346a584 authored by Prajwal Patil's avatar Prajwal Patil
Browse files

#9 Added the arrayfilter and upsert option in separate customeUpdate function

1 merge request!249#9 Added the arrayfilter and upsert option in separate customeUpdate function
Showing with 42 additions and 0 deletions
......@@ -515,6 +515,48 @@ func (mg *MongoDAO) PushData(selector map[string]interface{}, data interface{})
return nil
}
//custom update with extra options
func (mg *MongoDAO) CustomUpdateWithOptions(selector map[string]interface{}, data interface{}, updateOps map[string]interface{}) error {
session, sessionError := GetMongoConnection(mg.hostName)
if errormdl.CheckErr(sessionError) != nil {
return errormdl.CheckErr(sessionError)
}
if mg.hostName == "" {
mg.hostName = defaultHost
}
db, ok := config[mg.hostName]
if !ok {
return errormdl.Wrap("No_Configuration_Found_For_Host: " + mg.hostName)
}
collection := session.Database(db.Database).Collection(mg.collectionName)
ops := options.UpdateOptions{}
resMarshal, err := json.Marshal(updateOps)
if err != nil {
return errormdl.Wrap("cannot marshal:")
}
obj := gjson.ParseBytes(resMarshal)
loggermdl.LogError(obj)
if updateOps != nil {
if obj.Get("arrayFilter").Value() != nil {
arr := obj.Get("arrayFilter").Value().([]interface{})
ops.SetArrayFilters(options.ArrayFilters{
Filters: arr,
})
}
if obj.Get("upsert").Value() != nil {
ops.SetUpsert(obj.Get("upsert").Bool())
}
}
_, updateError := collection.UpdateMany(context.Background(), selector, data, &ops)
if errormdl.CheckErr1(updateError) != nil {
return errormdl.CheckErr1(updateError)
}
return nil
}
// CustomUpdate - CustomUpdate
func (mg *MongoDAO) CustomUpdate(selector map[string]interface{}, data interface{}) error {
session, sessionError := GetMongoConnection(mg.hostName)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment