Created
February 13, 2013 19:10
-
-
Save siygle/4947214 to your computer and use it in GitHub Desktop.
sample of mgo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "labix.org/v2/mgo" | |
| ) | |
| type Person struct { | |
| Name string | |
| Phone string | |
| Address string | |
| } | |
| func main() { | |
| session, err := mgo.Dial("localhost") | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer session.Close() | |
| session.SetMode(mgo.Monotonic, true) | |
| c := session.DB("mgo").C("contact") | |
| err = c.Insert( | |
| &Person{"Ale", "55556666", "Taiwan"}, | |
| &Person{"Keke", "37483748", "U.S"}, | |
| ) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println("Insert Success!") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main [3/1970] | |
| import ( | |
| "fmt" | |
| "labix.org/v2/mgo" | |
| "labix.org/v2/mgo/bson" | |
| ) | |
| type Person struct { | |
| Name string | |
| Phone string | |
| Address string | |
| } | |
| func main() { | |
| session, err := mgo.Dial("localhost") | |
| if err != nil { | |
| panic(err) | |
| } | |
| c := session.DB("mgo").C("contact") | |
| q := c.Find(bson.M{}) | |
| count, _ := q.Count() | |
| if count > 0 { | |
| result := Person{} | |
| iter := q.Iter() | |
| for iter.Next(&result) { | |
| fmt.Printf("Result: %v|%v|%v", result.Name, result.Phone, result.Address) | |
| } | |
| if iter.Err() != nil { | |
| panic(iter.Err()) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment