Golang uses mgo to connect to MongoDB

Source: Internet
Author: User
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 "})
} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.