MgO Operation MongoDB

Source: Internet
Author: User

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

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.