主要罗列出使用 go 语言操作 mongo 的一些细节和注意事项
基于go.mongodb.org/mongo-driver
连接 mongo-db
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
| package main
import ( "context" "fmt"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" )
var Db *mongo.Client
func main() { clientOptions := options.Client().ApplyURI("mongodb://192.168.41.11:6300")
client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { panic("mongodb创建失败" + err.Error()) }
err = client.Ping(context.TODO(), nil) if err != nil { panic("mongodb连接失败" + err.Error()) }
Db = client }
|
#