Github: github. comZhangzheBJUTblogblobmastermgo. at present, md does not officially release the golang driver officially supported by MongoDB. We recommend that you use the mgo driver. detailed description of mgo: godoc. orglabix. the following is a self-written version of orgv2mgo used to connect to the MongoDB database.
Github: https://github.com/ZhangzheBJUT/blog/blob/master/mgo.md now MongoDB official has not released about the official support of golang driver, it is recommended to use the mgo. the detailed document description of mgo: http://godoc.org/labix.org/v2/mgo below is a self-written I am developing Using mgo to connect to the MongoDB Database
Github: https://github.com/ZhangzheBJUT/blog/blob/master/mgo.md
MongoDB has not yet released the official support for golang driver, it is recommended to use the detailed documentation of mgo. mgo: http://godoc.org/labix.org/v2/mgo
The following is a self-developed instance used to connect to the MongoDB Database Using mgo.
Package mainimport ("fmt" "labix.org/v2/mgo" "labix.org/v2/mgo/bson") type Person struct {NAME string PHONE string} type Men struct {Persons [] Person} const = (URL = "192.168.2.175: 27017 ") func main () {session, err: = mgo. dial (URL) // connect to the database if err! = Nil {panic (err)} defer session. close () session. setMode (mgo. monotonic, true) db: = session. DB ("mydb") // Database Name collection: = db. C ("person") // If the set already exists, the system returns the number of elements in the/***** set ********* countNum, err: = collection. count () if err! = Nil {panic (err)} fmt. println ("Things objects count:", countNum) // ******** insert element ******** temp: = & Person {PHONE: "18811577546", NAME: "zhangzheHero"} // you can insert multiple objects at a time to insert two Person objects err = collection. insert (& Person {"Ale", "+ 55 53 8116 9639"}, temp) if err! = Nil {panic (err)} // ***** query a single data record ******* result: = Person {} err = collection. find (bson. M {"phone": "456 "}). one (& result) fmt. println ("Phone:", result. NAME, result. PHONE) // ****** query multiple data items ******* var personAll Men // Save the result iter: = collection. find (nil ). iter () for iter. next (& result) {fmt. printf ("Result: % v \ n", result. NAME) personAll. persons = append (personAll. persons, result)} // ******** update data ************ err = collection. update (bson. M {"name": "ccc"}, bson. M {"$ set": bson. M {"name": "ddd"}) err = collection. update (bson. M {"name": "ddd"}, bson. M {"$ set": bson. M {"phone": "12345678" }}) err = collection. update (bson. M {"name": "aaa"}, bson. MB {"phone": "1245", "name": "bbb "}) // ******************* _, err = collection. removeAll (bson. M {"name": "Ale "})
}