1 回答

TA貢獻1801條經驗 獲得超8個贊
問題在于您將 the 聲明operations
為 a?[]bson.M{}
,因此您獲得了一組地圖。
如果您檢查bson.M{} 的定義,它是一個 map[string]interface{} 您只需要將元素添加到地圖(在您的情況下是您想要的操作)。為此,語法是yourMap[yourKey] = yourValue
.
嘗試使用以下代碼生成操作循環:
operations := bson.M{}
for _, val := range conditions {
? ? var attr, operator, value interface{}
? ? cons := val.(map[interface{}]interface{})
? ? for range cons {
? ? ? ? attr? ? ?= cons["attribute"]
? ? ? ? operator = cons["operator"]
? ? ? ? value? ? = cons["value"]
? ? ? ? switch operator {
? ? ? ? ? ? case "==":
? ? ? ? ? ? ? ? operator = "$eq"
? ? ? ? ? ? case "!=":
? ? ? ? ? ? ? ? operator = "$ne"
? ? ? ? ? ? case "()":
? ? ? ? ? ? ? ? operator = "$in"
? ? ? ? }
? ? }
? ? ? ? operations[attr.(string)] = bson.M{operator.(string): value}
}
- 1 回答
- 0 關注
- 132 瀏覽
添加回答
舉報