Advertisement
Guest User

Untitled

a guest
Sep 25th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "gopkg.in/mgo.v2"
  6. "gopkg.in/mgo.v2/bson"
  7. "log"
  8. "os"
  9. )
  10.  
  11. type Person struct {
  12. Name string
  13. Email string
  14. }
  15.  
  16. func main() {
  17. // Do the following:
  18. // In a command window:
  19. // set MONGOLAB_URL=mongodb://IndianGuru:dbpassword@ds051523.mongolab.com:51523/godata
  20. // IndianGuru is my username, replace the same with yours. Type in your password.
  21. uri := os.Getenv("MONGOLAB_URL")
  22. if uri == "" {
  23. fmt.Println("no connection string provided")
  24. os.Exit(1)
  25. }
  26.  
  27. sess, err := mgo.Dial(uri)
  28. if err != nil {
  29. fmt.Printf("Can't connect to mongo, go error %v\n", err)
  30. os.Exit(1)
  31. }
  32. defer sess.Close()
  33.  
  34. sess.SetSafe(&mgo.Safe{})
  35.  
  36. collection := sess.DB("godata").C("user")
  37.  
  38. err = collection.Insert(&Person{"Stefan Klaste", "klaste@posteo.de"},
  39. &Person{"Nishant Modak", "modak.nishant@gmail.com"},
  40. &Person{"Prathamesh Sonpatki", "csonpatki@gmail.com"},
  41. &Person{"murtuza kutub", "murtuzafirst@gmail.com"},
  42. &Person{"aniket joshi", "joshianiket22@gmail.com"},
  43. &Person{"Michael de Silva", "michael@mwdesilva.com"},
  44. &Person{"Alejandro Cespedes Vicente", "cesal_vizar@hotmail.com"})
  45. if err != nil {
  46. log.Fatal("Problem inserting data: ", err)
  47. return
  48. }
  49.  
  50. result := Person{}
  51. err = collection.Find(bson.M{"name": "Prathamesh Sonpatki"}).One(&result)
  52. if err != nil {
  53. log.Fatal("Error finding record: ", err)
  54. return
  55. }
  56.  
  57. fmt.Println("Email Id:", result.Email)
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement