濮阳杆衣贸易有限公司

主頁 > 知識庫 > golang中使用mongo的方法介紹

golang中使用mongo的方法介紹

熱門標(biāo)簽:西部云谷一期地圖標(biāo)注 地圖標(biāo)注的汽車標(biāo) 高德地圖標(biāo)注口訣 廣州呼叫中心外呼系統(tǒng) 江西轉(zhuǎn)化率高的羿智云外呼系統(tǒng) 學(xué)海導(dǎo)航地圖標(biāo)注 中國地圖標(biāo)注省會高清 浙江高速公路地圖標(biāo)注 南通如皋申請開通400電話

前言

筆者使用的mongo驅(qū)動是mgo, 這個使用的人比較多,文檔也比較齊全

官網(wǎng)地址:http://labix.org/mgo

文檔地址:https://godoc.org/labix.org/v2/mgo

源碼地址:https://github.com/go-mgo/mgo

1. mgo包安裝

go get gopkg.in/mgo.v2

但是貌似現(xiàn)在從gopkg.in下載不了,迂回一下,先從github上下載

go get github.com/go-mgo/mgo

下載好了之后,在$GOPATH/src/下面創(chuàng)建文件夾gopkg.in/mgo.v2, 然后將github.com/go-mgo/mgo的內(nèi)容,拷貝到gopkg.in/mgo.v2

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:%s\n", 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: %d\n", 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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • golang操作mongodb的方法
  • Golang對MongoDB數(shù)據(jù)庫的操作簡單封裝教程

標(biāo)簽:吐魯番 貴州 德宏 常州 東營 許昌 曲靖 保定

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang中使用mongo的方法介紹》,本文關(guān)鍵詞  golang,中,使用,mongo,的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang中使用mongo的方法介紹》相關(guān)的同類信息!
  • 本頁收集關(guān)于golang中使用mongo的方法介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    温州市| 邵阳县| 修水县| 姚安县| 锡林浩特市| 眉山市| 连州市| 慈溪市| 克什克腾旗| 邯郸市| 三原县| 樟树市| 拜泉县| 贡觉县| 木兰县| 宁津县| 海晏县| 凉城县| 偃师市| 贡觉县| 彰化县| 房山区| 固安县| 隆尧县| 中卫市| 曲周县| 绥德县| 车险| 宝鸡市| 策勒县| 美姑县| 平度市| 天镇县| 沙雅县| 靖安县| 招远市| 静安区| 子洲县| 彰化市| 东宁县| 休宁县|