如何在golang中的mongo中将bson.M元素列表组合成单个bson.M?

Vinay Sawant:
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}}

相同键的所有操作都需要追加。谢谢

Vinay Sawant:
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
}

这是你可以尝试的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在JavaScript中将多个数组元素组合成单个对象

在JavaScript中将数组组合成单个对象

如何在Python中将列表和单个元素组合成一个元组?

如何在 Golang 中从 MongoDB 的 bson.M 生成多行字符串值?

如何在golang中编写bson形式的mongo查询?

将数组字段组合成单个数组字段 mongo

在 kotlin 中将对象数据值组合成单个字符串

如何将 Ubuntu/Debian 内核配置组合成单个配置

如何将对象键/值组合成单个数组?

如何将多个数组组合成单个JSON对象

如何将日期和时间组合成单个日期时间对象?

如何调用此函数从列表中随机选择一个数字,然后将“M”、“.”和“.wav”组合成一个文件名?

Excel Power Query:如何将所有列表项组合成单行

如何使用python将单个列的所有值组合成单个字符串变量?

在flutter中将字符串日期和字符串时间组合成单个DateTime类型

我如何将其组合成单线-bash

如何将多个 jquery 函数组合成单个自定义 jquery 文件

将不规则的 H:M:S 时间戳数据组合成 R 中的每小时间隔

质疑如何在Golang中将mongodb的命令转换为Bson

如何在javascript中解决此代码?将具有相同值的不同对象组合成单个数组

Python - 从“for循环”中获取单个浮点类型输出并组合成一个列表

如何从MongoDb查询中编写go bson.M

在jq中使用CSV输出时,如何将数组组合成单个字符串值?

如何检索[] bson.M类型的地图

如何使用mongo-go-driver将Bson文档解组为M型

如何将行组合成单独的数据框 python pandas

如何在Python中将两个列表组合成字典?

如何使用Golang比较两个bson.M数据集

在python中组合成一个平面列表