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?
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 int
s, 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, eg
test[key] = (value.([]int))[0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.