Golang 的 MongoDB 数据库链接及使用

mGo

1
2
3
4
5
6
7
> 推荐使用, 方法全
go get gopkg.in/mgo.v2
import "gopkg.in/mgo.v2"

> 不推荐使用, 有些方法没有
go get labix.org/v2/mgo
import "labix.org/v2/mgo"

官文: https://godoc.org/gopkg.in/mgo.v2

Golang连接MongoDB数据库与基本操作

实现对于MongoDB的正则表达式查找

Makefile

1
2
3
4
5
6
7
8
Proj=github.com/NICKNAME/mongo-demo

all:
go build ${PROJ}
./mongo-demo

test:
go test

chain/block.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package chain

import (
"fmt"
)

type Block struct {
TxHash string `json:"txhash"`
Id string `json:"id"`
Phone string `json:"phone"`
}

func (block *Block) GetTxHash() (txHash string){
fmt.Println("block chain: ", block.TxHash)

return block.TxHash
}

func (block *Block) ToMap() map[string]interface{} {
var blockMap = make(map[string]interface{})

blockMap["txhash"] = block.TxHash
blockMap["id"] = block.Id
blockMap["phone"] = block.Phone

return blockMap
}

connect.go 测试 demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main

import (
"fmt"
// "errors"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/NICKNAME/mongo-demo/chain"
)

const (
MONGODB_URL = "mongodb://localhost:27017/chain"
DB_NAME = "chain"
)

func errPanic(err error) {
if err != nil {
fmt.Println("Got Error:", err)
panic(err)
}
}

func errRecover() {
if r := recover(); r != nil {
fmt.Println("Recover From Error", r)
}
}

func main() {
defer errRecover()

session, err := mgo.Dial(MONGODB_URL)
errPanic(err)
defer session.Close()

session.SetMode(mgo.Monotonic, true)
db := session.DB(DB_NAME)
blockdb := db.C("blocks")
fmt.Println("DB:", db, " Blockdb:", blockdb)

// insert
err = blockdb.Insert(
&chain.Block{"0x123", "123", "10000"},
&chain.Block{"0x124", "124", "10010"},
&chain.Block{"0x125", "125", "10086"},
)
errPanic(err)

// count
count, err := blockdb.Count()
errPanic(err)
fmt.Println("Count:", count)

// find
block := chain.Block{}
err = blockdb.Find(bson.M{"txhash": "0x123"}).One(&block)
errPanic(err)
fmt.Println("Data:", block)

// find all, iter -> Fits Large Data Set ^o^
item := &chain.Block{}
it := blockdb.Find(nil).Iter()
for it.Next(item) {
fmt.Println("Loop Item: ", item.TxHash, item.Id, item.Phone)
}
// find all, array -> When Large Data Set, DO NOT Use This!
var allBlocks []chain.Block
err = blockdb.Find(nil).All(&allBlocks)
errPanic(err)
for i:=0; i<len(allBlocks); i++ {
fmt.Println("Loop Item: ", allBlocks[i])
}

// update
err = blockdb.Update(bson.M{"txhash": "0x123"}, bson.M{"$set": bson.M{"phone": "13301001234"}})
errPanic(err)
err = blockdb.Find(bson.M{"txhash": "0x123"}).One(&block)
errPanic(err)
fmt.Println("Updata:", block)

// remove && remove all
err = blockdb.Remove(bson.M{"id": "123"})
errPanic(err)
removed, err := blockdb.RemoveAll(bson.M{"id": bson.M{"$regex": "^12*", "$options": "$i"}})
errPanic(err)
fmt.Println("Updated:", removed.Updated, "Removed:", removed.Removed, "Matched:", removed.Matched)
count, err = blockdb.Find(bson.M{"id": "123"}).Count()
errPanic(err)
fmt.Println("After Remove:", count)

}
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.