Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何在golang的mongo中将bson.M元素列表合并为单个bson.M?_Mongodb_Go_Bson_Mgo - Fatal编程技术网

Mongodb 如何在golang的mongo中将bson.M元素列表合并为单个bson.M?

Mongodb 如何在golang的mongo中将bson.M元素列表合并为单个bson.M?,mongodb,go,bson,mgo,Mongodb,Go,Bson,Mgo,} 同一个密钥的所有操作都需要追加。提前感谢 I want the in this format : bson.M{"$inc": bson.M{"Google.ab.Value": 1, "AB.Value.to": 2}} 这就是您可以尝试的为什么键“AB.Value.to”增加了2?这是一种类型,还是因为它是第二个添加的类型? I want the in this format : bson.M{"$inc": bson.M{"Google.ab.Value": 1, "AB.V

}

同一个密钥的所有操作都需要追加。提前感谢

 I want the in this format :
 bson.M{"$inc": bson.M{"Google.ab.Value": 1, "AB.Value.to": 2}}
这就是您可以尝试的

为什么键“AB.Value.to”增加了2?这是一种类型,还是因为它是第二个添加的类型?
 I want the in this format :
 bson.M{"$inc": bson.M{"Google.ab.Value": 1, "AB.Value.to": 2}}
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
}