1, to install the MongoDB database, 2, to download Golang MgO package, 3, connection operation
Package Mainimport ("Gopkg.in/mgo.v2" "Gopkg.in/mgo.v2/bson" "FMT") const URL = "192.168.1.83:27017"//mongodb address func Main () {session, Err: = MgO. Dial (URL)//Connect server if Err! = Nil {panic (err)}c: = Session. DB ("chatroom"). C ("account")//Select the Account table of the chatroom library C.insert (map[string]interface{}{"id": 7, "name": "Tongjh", "Age": 25})//add ObjID: = Bson. Objectidhex ("55b97a2e16bc6197ad9cad59") C.removeid (ObjID)//delete C. UpdateID (ObjID, map[string]interface{}{"id": 8, "name": "AAAAA", "Age": 30})//var one map[string]interface{}c.findid ( ObjID). One (&one)//query qualifying row of data fmt. Println (one) var result []map[string]interface{}c.find (nil). All (&result)//Search all FMT. PRINTLN (Result)}
Golang manipulating MongoDB