《Web Development with Go》Mangodb查询一条记录
作者:互联网
select加where
package main import ( "fmt" "log" "time" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type Task struct { Description string Due time.Time } type Category struct { Id bson.ObjectId `bson:"_id,omitempty"` Name string Description string Tasks []Task } func main() { mongoDialInfo := &mgo.DialInfo{ Addrs: []string{"localhost"}, Timeout: 5 * time.Second, Database: "taskdb", Username: "root", Password: "123456", } session, err := mgo.DialWithInfo(mongoDialInfo) if err != nil { panic(err) } defer session.Close() session.SetMode(mgo.Monotonic, true) c := session.DB("taskdb").C("categories") result := Category{} err = c.Find(bson.M{"name": "Open-Source"}).One(&result) if err != nil { log.Fatal(err) } fmt.Printf("Category: %s, Description: %s\n", result.Name, result.Description) tasks := result.Tasks for _, v := range tasks { fmt.Printf("Task: %s Due: %v\n", v.Description, v.Due) } }
标签:Development,Web,Description,err,mgo,session,result,Mangodb,bson 来源: https://www.cnblogs.com/aguncn/p/12005386.html