首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >蒙古司机未能连接

蒙古司机未能连接
EN

Stack Overflow用户
提问于 2019-02-20 03:47:18
回答 2查看 11.3K关注 0票数 1

因此,我试图使用https://github.com/mongodb/mongo-go-driver连接到戈朗的mongo数据库。

下面是我的连接处理程序:

代码语言:javascript
复制
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
}

我所犯的错误是:

代码语言:javascript
复制
api         | database/connection.go:29:30: cannot use uri (type 
string) as type *options.ClientOptions in argument to mongo.Connect

因此,我尝试用如下所示的连接字符串替换uri

代码语言:javascript
复制
client, err := mongo.Connect(ctx, "mongodb://datastore:27017")

但我还是犯了错误。

将其与文档中的内容进行比较:

ctx,_ := context.WithTimeout(context.Background(),10*time.Second)客户机,err := mongo.Connect(ctx,"mongodb://localhost:27017")

而且完全一样!我真的不知道为什么会有这个错误。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-20 04:24:47

对于那些来搜索的人来说--这些文档在这篇文章中已经过时了,但是他们最近的努力是:https://github.com/mongodb/mongo-go-driver/commit/32946b1f8b9412a6a94e68ff789575327bb257cf让他们用连接来完成这个任务:

代码语言:javascript
复制
client, err := mongo.NewClient(options.Client().ApplyURI(uri))

您现在还需要导入选项包。黑客很开心。

编辑:谢谢vcanales的发现--你是个绅士和学者。

票数 8
EN

Stack Overflow用户

发布于 2019-06-27 14:53:51

除了接受的答案之外,下面的这个片段可以通过使用一个环境变量来传递Mongodb来改进。

代码语言:javascript
复制
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的更多信息:https://docs.mongodb.com/manual/reference/method/cursor.readPref/index.html https://docs.mongodb.com/manual/core/read-preference/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54778520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档