Using MongoDB in Golang

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

About MgO

MongoDB officials do not have the drive to go MongoDB, so only use third-party drivers, MgO is the most used one.
MgO (sound mango) is a go language driver for MongoDB, which is rich in features and is well tested with a simple API based on GO syntax.

Official website: Http://labix.org/mgo

Installation and use

Installation

go get gopkg.in/mgo.v2

Use in Go

package modelsimport ("gopkg.in/mgo.v2""log")type Person struct {Name  stringPhone string}const URL = "127.0.0.1:27017"func AddPerson() string {session, err := mgo.Dial(URL) //连接数据库if err != nil {log.Fatal(err)}defer session.Close()session.SetMode(mgo.Monotonic, true)db := session.DB("mydb")collection := db.C("person")err = collection.Insert(&Person{"Ale", "+55 53 8116 9639"}, &Person{"Cla", "+55 53 8402 8510"})if err != nil {log.Fatal(err)}return "true"}

See red Elephant ued for more information

Related Article

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.