Database Mongo Go驱动程序无法连接
因此,我试图使用连接到golang的mongo数据库 这是我的连接处理程序:Database Mongo Go驱动程序无法连接,database,mongodb,go,database-connection,mongo-go,Database,Mongodb,Go,Database Connection,Mongo Go,因此,我试图使用连接到golang的mongo数据库 这是我的连接处理程序: var DB *mongo.Database func CreateConnectionHandler()(*mongo.Database, error){ fmt.Println("inside createConnection in database package") godotenv.Load() fmt.Println("in CreateConnectionHandler and
var DB *mongo.Database
func CreateConnectionHandler()(*mongo.Database, error){
fmt.Println("inside createConnection in database package")
godotenv.Load()
fmt.Println("in CreateConnectionHandler and SERVER_CONFIG: ")
fmt.Println(os.Getenv("SERVER_CONFIG"))
uri:=""
if os.Getenv("SERVER_CONFIG")=="kubernetes"{
fmt.Println("inside kubernetes db config")
uri = "mongodb://patientplatypus:SUPERSECRETPASSDOOT@
mongo-release-mongodb.default.svc.cluster.local:27017/
platypusNEST?authMechanism=SCRAM-SHA-1"
}else if os.Getenv("SERVER_CONFIG")=="compose"{
fmt.Println("inside compose db config")
uri = "mongodb://datastore:27017"
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, uri)
if err != nil {
return nil, fmt.Errorf("mongo client couldn't connect: %v", err)
}
DB := client.Database("platypusNEST")
return DB, nil
}
我得到的错误是:
api | database/connection.go:29:30: cannot use uri (type
string) as type *options.ClientOptions in argument to mongo.Connect
因此,我尝试用如下连接字符串替换uri
:
client, err := mongo.Connect(ctx, "mongodb://datastore:27017")
但我还是犯了错误
将其与文档中的内容进行比较:
而且完全一样!我真的不知道为什么会有这个错误。有什么想法吗?给那些来搜索的人-这些文档在本帖发布时已经过时了,但是他们最近推到这里:让他们用connect做这件事:
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
现在还需要导入选项包。快乐黑客
编辑:感谢vcanales找到这篇文章-你是一位绅士和学者。对于那些前来搜索的人来说,这些文档在本帖发布时已经过时,但他们最近推到这里:让他们使用connect:
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
现在还需要导入选项包。快乐黑客
编辑:感谢vcanales找到这篇文章-您是一位绅士和学者。除了公认的答案之外,下面的这篇文章可以通过使用环境变量传入Mongodb URL来改进
package main
import (
"context" //use import withs " char
"fmt"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func ConnectMongo() {
var (
client *mongo.Client
mongoURL = "mongodb://localhost:27017"
)
// Initialize a new mongo client with options
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURL))
// Connect the mongo client to the MongoDB server
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
// Ping MongoDB
ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
if err = client.Ping(ctx, readpref.Primary()); err != nil {
fmt.Println("could not ping to mongo db service: %v\n", err)
return
}
fmt.Println("connected to nosql database:", mongoURL)
}
func main() {
ConnectMongo()
}
有关选项和readpref的详细信息:
除了公认的答案之外,下面的代码片段还可以通过使用环境变量传入Mongodb URL进行改进
package main
import (
"context" //use import withs " char
"fmt"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func ConnectMongo() {
var (
client *mongo.Client
mongoURL = "mongodb://localhost:27017"
)
// Initialize a new mongo client with options
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURL))
// Connect the mongo client to the MongoDB server
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
// Ping MongoDB
ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
if err = client.Ping(ctx, readpref.Primary()); err != nil {
fmt.Println("could not ping to mongo db service: %v\n", err)
return
}
fmt.Println("connected to nosql database:", mongoURL)
}
func main() {
ConnectMongo()
}
有关选项和readpref的详细信息:
遵循github的最新步骤。使用导入“go.mongodb.org/mongo driver/mongo”后,出现错误“options.Client().ApplyURI未定义(type*options.ClientOptions没有字段或方法ApplyURI)”。。。选项取自github.com/mongodb/mongo-go-driver/mongo/options答案过时。最新答案可在此处找到:遵循github的最新步骤。使用导入“go.mongodb.org/mongo driver/mongo”后,出现错误“options.Client().ApplyURI未定义(type*options.ClientOptions没有字段或方法ApplyURI)”。。。选项取自github.com/mongodb/mongo-go-driver/mongo/options答案过时。最新答案可在此处找到: