MgO Basic use:
Http://labix.org/mgo
Installation
# go get gopkg.in/mgo.v2
Package Mainimport ("FMT", "Log" "Gopkg.in/mgo.v2" "Gopkg.in/mgo.v2/bson") type person struct { Name string Phone string}func main () {//mongodb connection URI//format is [mongodb://][user:[email protecte d]]host1[:p ort1][,host2[:p Ort2],...] [/database] [? options] Session, ERR: = MgO. Dial ("Mongodb://cord:[email protected]:27010/keymap") if err! = Nil {panic (err)} Defer session. Close ()//Optional. Switch the session to a monotonic behavior. Session. SetMode (MgO. Monotonic, True) c: = Session. DB ("Test"). C ("people")//Add Record err = C.insert (&person{"Ale", "+55 8116 9639"}, &per son{"Cla", "+55 8402 8510"}) if err! = Nil {log. Fatal (ERR)} Result: = person{}//Normal query err = C.find (Bson. m{"name": "Ale"}). One (&result)//Regular match (fuzzy query), where "$options": "$i" means ignoring case//regexstr = "A" Err = C.find (Bson. m{"name": Bson. m{"$regex": Regexstr, "$options": "$i"}}). All (&result) if err! = Nil {log. Fatal (Err)} FMT. Println ("Phone:", result.) Phone)//delete Record err = C.remove (Bson. m{"name": "Ale"})//delete single, if there are more than one, the default order first _, err = C.removeall (Bson. m{"name": "Ale"})//delete all lookups if err! = Nil {fmt. PRINTLN (ERR)}}
MgO Operation MongoDB