1

I write the code with Go to get value from specify key, but it's return only `_idà value from my code

func main() {
    mongoDialInfo := &mgo.DialInfo{
        Addrs:    []string{"localhost:27017"},
        Database: "person",
        Username: "user",
        Password: "user",
        Timeout:  60 * time.Second,
    }
    session, err := mgo.DialWithInfo(mongoDialInfo)
    if err != nil {
        panic(err)
    }
    defer session.Close()

    c := session.DB("person").C("person")

    result := Person{}
    name := "Bill"

    err = c.Find(bson.M{"name": name}).Select(bson.M{"surname": 1 }).One(&result)

    if err != nil {
        panic(err)
    }

    fmt.Println("Surname is ", result)
}

and the result from code is

{ObjectIdHex("5b9bb0b39b5c3e5733e1c8f7") }

Please help me to fix it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Neung Kittipong
  • 85
  • 1
  • 1
  • 12

0 Answers0