doutizhou5312 2017-05-11 12:26
浏览 114

如何使用mgo从mongodb获取的切片中设置所有数据?

I am trying to fetch data from MongoDB that has dynamic keys and set it in a slice.

Here is my data sample:

_id ObjectIdHex("5911786dc28f25578150501d")
2017-05-01 [800 1000 1200 1400 1600]
_id ObjectIdHex("59117897c28f25578150501e")
2017-05-02 [800 1000 1200 1400 1600]
_id ObjectIdHex("5911789ec28f25578150501f")
2017-05-03 [800 1000 1200 1400 1600]
2017-05-04 [800 1000 1200 1400 1600]
_id ObjectIdHex("591178a6c28f255781505020")
_id ObjectIdHex("591178abc28f255781505021")
2017-05-05 [800 1000 1200 1400 1600]
_id ObjectIdHex("591178b0c28f255781505022")
2017-05-06 [800 1000 1200 1400 1600]
_id ObjectIdHex("591178b5c28f255781505023")
2017-05-07 [800 1000 1200 1400 1600]
_id ObjectIdHex("591178bac28f255781505024")
2017-05-08 [800 1000 1200 1400 1600]
_id ObjectIdHex("591178c8c28f255781505025")
2017-05-09 [800 1000 1200 1400 1600]
2017-05-10 [800 1000 1200 1400 1600]

I need to set it into an array like {2017-05-09 : [800 1000 1200 1400 1600]} and same for other entries.

I have tried

package main

import(
    "fmt"
    "gopkg.in/mgo.v2/bson"
    //"encoding/json"
)

type Spot struct{
    Id      bson.ObjectId   `json:"_id,omitempty" bson:"_id,omitempty"`
    Spots   map[string]interface{} `json:"spots"`
}

//type Values []Value

//var result []struct{ Value int }
type Spots []Spot

func getAllSpots() (Spots) {
    mongoSession := getDbSession()

    sessionCopy := mongoSession.Copy()
    defer sessionCopy.Close()
    var spots []Spot
    c := mongoSession.DB("test").C("spots")
    var data []bson.M
    err := c.Find(bson.M{}).All(&data)
    if err != nil {
        panic(err)
        // TODO: Do something about the error
    }

    test := make(map[string]int)
    for _, doc := range data {
      for key, value := range doc {
        if(key == "_id"){
            test[key] = value
            fmt.Println(key, value)
        }
      }
    }    

    return spots
}

I am able to get the spots in data and able to write the output to the console using fmt.Println() but when I assign it to a slice it gives me the following error:

cannot use value (type interface {}) as type int in assignment: need type assertion

I searched all over the web but couldn't find a valid solution. Can anyone please guide me what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • drq61040 2017-05-11 14:19
    关注

    If you look at the documentation for bson.M, you'll see that it's just an alias for map[string]interface{}. That means when you range over it, your key is a string, and your value is an interface{}. Your target is a map[string]int. So when you test[key] = value, you're trying to assign value (an interface{}) to something expecting an int, which you can't do without an explicit cast (aka type assertion). This is exactly what the error message says: cannot use value (type interface {}) as type int in assignment: need type assertion. You could instead do:

    test[key] = value.(int)
    

    But as a putu noted, it looks like those values are actually arrays of ints, which doesn't fit the test type at all, as it is a map of strings to single int values. So you'd need to either change the type of test to map[string][]int or pick a value from the source array to store in the map, e.g.

    test[key] = (value.([]int))[0]
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!