如何在Golang中将bson.M元素的列表合并为单个bson.M对象?

huangapple go评论89阅读模式
英文:

How do i combine list of bson.M elements into a single bson.M in mongo in golang?

问题

lstMap := make([]interface{}, 0)
lstMap = mongoOps.AddToBsonMap(lstMap, bson.M{"$inc": bson.M{"Google.ab.Value": 1}})
lstMap = mongoOps.AddToBsonMap(lstMap, bson.M{"$inc": bson.M{"Google.ab1.Value1": 1}})

func (o *MongoOps) AddToBsonMap(lstMap []interface{}, value interface{}) (result []interface{}) {
lstMap = append(lstMap, value)
return lstMap
}

我想要的格式是:
bson.M{"$inc": bson.M{"Google.ab.Value": 1, "AB.Value.to": 2}}
相同键的所有操作需要被追加。谢谢你的帮助。

英文:
lstMap := make([]interface{}, 0)
lstMap = mongoOps.AddToBsonMap(lstMap, bson.M{"$inc": bson.M{"Google.ab.Value": 1}})
lstMap = mongoOps.AddToBsonMap(lstMap, bson.M{"$inc": bson.M{"Google.ab1.Value1": 1}})

func (o *MongoOps) AddToBsonMap(lstMap []interface{}, value interface{}) (result []interface{}) {
lstMap = append(lstMap, value)
return lstMap

}

 I want the in this format :
 bson.M{"$inc": bson.M{"Google.ab.Value": 1, "AB.Value.to": 2}}

All the operation of same key needs to be appended.Thank you in advance

答案1

得分: 0

func (o *MongoOps) AddToBsonMapElement(lstMap map[string]interface{}, Operation string, key string, value interface{}) (result map[string]interface{}) {
status, msg := EmptyStructCheck(o)
if status == true {
LogError(msg)
panic(msg)
}
if Operation == "$addToSetEach" {
if _, ok := lstMap["$addToSet"]; ok {
childmap := lstMap["$addToSet"]
subchildmap := childmap.(map[string]interface{})
var val map[string]interface{}
val = make(map[string]interface{})
val["$each"] = value
subchildmap[key] = val
lstMap["$addToSet"] = subchildmap

	} else {
		lstMap["$addToSet"] = bson.M{key: bson.M{"$each": value}}
	}

	fmt.Println(reflect.TypeOf(lstMap))
} else if _, ok := lstMap[Operation]; ok {
	childmap := lstMap[Operation]
	subchildmap := childmap.(map[string]interface{})
	subchildmap[key] = value
	lstMap[Operation] = subchildmap

} else {
	childmap := make(map[string]interface{}, 0)
	childmap[key] = value
	lstMap[Operation] = childmap
}

return lstMap

}

英文:
func (o *MongoOps) AddToBsonMapElement(lstMap map[string]interface{},     Operation string, key string, value interface{}) (result map[string]interface{})     {
status, msg := EmptyStructCheck(o)
if status == true {
	LogError(msg)
	panic(msg)
}
if Operation == "$addToSetEach" {
	if _, ok := lstMap["$addToSet"]; ok {
		childmap := lstMap["$addToSet"]
		subchildmap := childmap.(map[string]interface{})
		var val map[string]interface{}
		val = make(map[string]interface{})
		val["$each"] = value
		subchildmap[key] = val
		lstMap["$addToSet"] = subchildmap

	} else {
		lstMap["$addToSet"] = bson.M{key: bson.M{"$each": value}}
	}

	fmt.Println(reflect.TypeOf(lstMap))
} else if _, ok := lstMap[Operation]; ok {
	childmap := lstMap[Operation]
	subchildmap := childmap.(map[string]interface{})
	subchildmap[key] = value
	lstMap[Operation] = subchildmap

} else {
	childmap := make(map[string]interface{}, 0)
	childmap[key] = value
	lstMap[Operation] = childmap
}

return lstMap
}

This is what you can try

huangapple
  • 本文由 发表于 2016年2月15日 16:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35404841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定