golang中使用mongo的方法介绍

2019-10-06 08:01 来源:未知

笔者使用的mongo驱动是mgo, 这么些应用的人可比多,文书档案也正如完备

前言

1.安装golang
https://golang.org/dl/](https://golang.org/dl/

  • 官方网站地址:
  • 文书档案地址:
  • 源码地址:

作者使用的mongo驱动是mgo, 那几个利用的人可比多,文书档案也正如完备

2.配备蒙受
GOROOT,GOPATH=“E:/go/work/”

官方网站地址:

3.下载相关的遭逢
git clone https://github.com/name5566/leafserver
go get github.com/name5566/leaf 或者 git clone https://github.com/name5566/leaf E:/go/work/src/github.com/name5566/leaf
go get www.weide1946.com,github.com/golang/protobuf/proto 或者 git clone https://github.com/golang/protobuf E:/go/work/src/github.com/golang/protobuf
go get gopkg.in/mgo.v2 或者 git clone https://gopkg.in/mgo.v2 E:/go/work/src/gopkg.in/mgo.v2
go get github.com/gorilla/websocket 或者 git clone https://github.com/gorilla/websocket E:/go/work/src/github.com/gorilla/websocket

go get gopkg.in/mgo.v2

文档地址:

4.编写翻译安装
go install server
备注,编写翻译进程中出现难点,e.g:

但是平日以往从gopkg.in下载不了,迂回一下,先从github上下载

源码地址:

www.weide1946.com 1

go get github.com/go-mgo/mgo

1. mgo包安装

image.png

下载好了随后,在$GOPATH/src/上边创立文件夹gopkg.in/mgo.v2, 然后将github.com/go-mgo/mgo的内容,拷贝到gopkg.in/mgo.v2

go get gopkg.in/mgo.v2

直接去下载对应的库就能够

mongo url 格式[mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]for examplemongodb://myuser:mypass@localhost:40001,otherhost:40001/mydb

可是平时现在从gopkg.in下载不了,迂回一下,先从github上下载

5.运行

// mongo_test project main.gopackage mainimport ( "fmt" "math/rand" "time" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson")type GameReport struct { // id bson.ObjectId `bson:"_id"` Game_id int64 Game_length int64 Game_map_id string}func err_handler(err error) { fmt.Printf("err_handler, error:%sn", err.Error panic(err.Error}func main() { dail_info := &mgo.DialInfo{ Addrs: []string{"127.0.0.1"}, Direct: false, Timeout: time.Second * 1, Database: "game_report", Source: "admin", Username: "test1", Password: "123456", PoolLimit: 1024, } session, err := mgo.DialWithInfo(dail_info) if err != nil { fmt.Printf("mgo dail error[%s]n", err.Error err_handler } defer session.Close() // set mode session.SetMode(mgo.Monotonic, true) c := session.DB("game_report").C("game_detail_report") r := rand.New(rand.NewSource(time.Now().UnixNano report := GameReport{ // id: bson.NewObjectId(), Game_id: 100, Game_length: r.Int63() % 3600, Game_map_id: "hello", } err = c.Insert if err != nil { fmt.Printf("try insert record error[%s]n", err.Error err_handler } result := GameReport{} var to_find_game_id int64 = 100 err = c.Find(bson.M{"game_id": to_find_game_id}).One(&result) if err != nil { fmt.Printf("try find record error[%s]n", err.Error err_handler } fmt.Printf("res, game_id[%d] length[%d] game_map_id[%s]n", to_find_game_id, result.Game_length, result.Game_map_id) // try find all report var results []GameReport err = c.Find.All(&results) if err != nil { fmt.Printf("try game all record of game_detail_report error[%s]n", err.Error err_handler } result_count := len fmt.Printf("result count: %dn", result_count) for i, report := range results { fmt.Printf("index: %d, report{ game_id: %d, game_length: %d, game_map_id: %s}n", i, report.Game_id, report.Game_length, report.Game_map_id) }}
go get github.com/go-mgo/mgo

www.weide1946.com 2

那边要留意的有些是 GameReport 里面包车型客车字段都要首字母大写,不然不会写入mongo

下载好了未来,在$GOPATH/src/上面创造文件夹gopkg.in/mgo.v2, 然后将github.com/go-mgo/mgo的源委,拷贝到gopkg.in/mgo.v2

image.png

2. 测量试验代码

// mongo_test project main.go
package main

import (
 "fmt"
 "math/rand"
 "time"

 "gopkg.in/mgo.v2"
 "gopkg.in/mgo.v2/bson"
)

type GameReport struct {
 // id   bson.ObjectId `bson:"_id"`
 Game_id  int64
 Game_length int64
 Game_map_id string
}

func err_handler(err error) {
 fmt.Printf("err_handler, error:%sn", err.Error())
 panic(err.Error())
}

func main() {
 dail_info := &mgo.DialInfo{
  Addrs:  []string{"127.0.0.1"},
  Direct: false,
  Timeout: time.Second * 1,
  Database: "game_report",
  Source: "admin",
  Username: "test1",
  Password: "123456",
  PoolLimit: 1024,
 }

 session, err := mgo.DialWithInfo(dail_info)
 if err != nil {
  fmt.Printf("mgo dail error[%s]n", err.Error())
  err_handler(err)
 }

 defer session.Clone()

 // set mode
 session.SetMode(mgo.Monotonic, true)

 c := session.DB("game_report").C("game_detail_report")

 r := rand.New(rand.NewSource(time.Now().UnixNano()))

 report := GameReport{
  // id:   bson.NewObjectId(),
  Game_id:  100,
  Game_length: r.Int63() % 3600,
  Game_map_id: "hello",
 }

 err = c.Insert(report)

 if err != nil {
  fmt.Printf("try insert record error[%s]n", err.Error())
  err_handler(err)
 }

 result := GameReport{}
 var to_find_game_id int64 = 100
 err = c.Find(bson.M{"game_id": to_find_game_id}).One(&result)
 if err != nil {
  fmt.Printf("try find record error[%s]n", err.Error())
  err_handler(err)
 }

 fmt.Printf("res, game_id[%d] length[%d] game_map_id[%s]n",
  to_find_game_id, result.Game_length, result.Game_map_id)

 // try find all report
 var results []GameReport
 err = c.Find(bson.M{}).All(&results)
 if err != nil {
  fmt.Printf("try game all record of game_detail_report error[%s]n",
   err.Error())
  err_handler(err)
 }

 result_count := len(results)
 fmt.Printf("result count: %dn", result_count)
 for i, report := range results {
  fmt.Printf("index: %d, report{ game_id: %d, game_length: %d, game_map_id: %s}n",
   i, report.Game_id, report.Game_length, report.Game_map_id)
 }
}

如此这般要介怀的一些是 GameReport 里面包车型客车字段都要首字母大写,不然不会写入mongo

总结

如上正是那篇文章的全部内容了,希望本文的源委对我们的学习或许工作有所一定的参照学习价值,如若不不奇怪我们能够留言交流,感谢大家对台本之家的支撑。

你或许感兴趣的篇章:

  • golang操作mongodb的方法
  • Golang对MongoDB数据库的操作简易包装教程
版权声明:本文由韦德娱乐1946_韦德娱乐1946网页版|韦德国际1946官网发布于网络编程,转载请注明出处:golang中使用mongo的方法介绍