|
@@ -1,29 +0,0 @@
|
|
|
-package mdb
|
|
|
-
|
|
|
-import (
|
|
|
- "context"
|
|
|
-
|
|
|
- "go.mongodb.org/mongo-driver/mongo"
|
|
|
- "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
-)
|
|
|
-
|
|
|
-var MdbAdmin *mongo.Database
|
|
|
-
|
|
|
-// InitializeMongoDB 初始化 MongoDB 连接
|
|
|
-func InitializeMongoDB() {
|
|
|
- // 设置 MongoDB 的连接选项
|
|
|
- clientOptions := options.Client().ApplyURI("mongodb://192.168.0.68:27017")
|
|
|
- // 创建一个新的 MongoDB 客户端
|
|
|
- client, err := mongo.Connect(context.Background(), clientOptions)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- // 检查连接是否成功
|
|
|
- err = client.Ping(context.Background(), nil)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- // 选择要使用的数据库
|
|
|
- db := client.Database("db_user_daily") // 替换为你的数据库名称
|
|
|
- MdbAdmin = db
|
|
|
-}
|