Skip to content

Instantly share code, notes, and snippets.

@siygle
Created February 13, 2013 19:10
Show Gist options
  • Select an option

  • Save siygle/4947214 to your computer and use it in GitHub Desktop.

Select an option

Save siygle/4947214 to your computer and use it in GitHub Desktop.
sample of mgo
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!")
}
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