简体   繁体   English

如何使用golang连接撰写mongodb

[英]how to connect compose mongodb with golang

I try to connect compose mongodb in bluemix with golang program but i get unsupported connection url option ssl. 我尝试使用golang程序在bluemix中连接撰写mongodb,但是我得到了不受支持的连接url选项ssl。 Here I give mongodb connection string got from composedb console. 在这里,我给出了从compositionb控制台获得的mongodb连接字符串。 How to connect with remote host need syntax for connect? 如何与远程主机连接需要连接的语法?

session, err := mgo.Dial("mongodb://****:****@aws-us-east-1-portal.26.dblayer.com:20258/admin?ssl=true")
    if err != nil {
        panic(err)
    }
    defer session.Close()

    // Optional. Switch the session to a monotonic behavior.
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("test").C("people")
    err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
        &Person{"Cla", "+55 53 8402 8510"})
    if err != nil {
        log.Fatal(err)
    }

The error: 错误:

panic: unsupported connection URL option: ssl=true

goroutine 1 [running]:
panic(0x2130a0, 0xc82000a840)
    /usr/local/go/src/runtime/panic.go:481 +0x3e6
main.main()
    /Users/vit/gocode/src/mongmix/mmix.go:19 +0x9b
exit status 2

install "go get gopkg.in/mgo.v2" 安装“ go get gopkg.in/mgo.v2”

package main
///////////////import mongo gopkg//////////////
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
//////////// add struct//////////////////////////
type Person struct {
Name string
Phone string
}
/////  main function///////////
func main(){
fmt.Println("Hello world");
//////db connection ////////////
session, err := mgo.Dial("localhost:9494")
if err != nil {
        panic(err)
}
defer session.Close()

// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
///////////////db name/////////////
c := session.DB("sudheer").C("people")

err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
           &Person{"Cla", "+55 53 8402 8510"})
if err != nil {
        log.Fatal(err)
}


result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil {
        log.Fatal(err)
 }

fmt.Println("Phone:", result.Phone)
 }

IBM Bluemix Compose for Mongodb has a documentation available online here: https://console.bluemix.net/docs/services/ComposeForMongoDB/index.html#getting-started-with-compose-for-mongodb IBM Bluemix Compose for Mongodb在此处在线提供了文档: https ://console.bluemix.net/docs/services/ComposeForMongoDB/index.html#getting-started-with-compose-for-mongodb

Step 2 talks about connecting to your compose mongo db. 第2步讨论有关连接到compose mongo db的问题。 There is a nodejs sample code provided in step 2. You will have to check connection string to mongodb in golang ie the syntax in golang. 在第2步中提供了一个nodejs示例代码。您将必须在golang中检查与mongodb的连接字符串,即golang中的语法。

I found out this article which explains connecting to IBM Compose mongodb from goland: https://www.compose.com/articles/connect-to-mongo-3-2-on-compose-from-golang/ 我发现了这篇文章,该文章解释了如何从goland连接到IBM Compose mongodb: https ://www.compose.com/articles/connect-to-mongo-3-2-on-compose-from-golang/

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM