am trying to connect to MongoDB via Go lang REST API. Trying to insert a movie. But, the connection to MongoDB is failing. The Golang REST API boom states to write 127.0.0.1 , I tried writing both localhost and 127.0.0.1 but still it fails, I mean in VSCODE how to connect mongodb, which is running on windows to golang running in VSCODE. I installed all mongodb extensions in VSCODE.
Error I am facing on VSCODE:
PS D:\goprojects\GO_RESTAPI_MONGDB> go mod init mgo.v2
go: creating new go.mod: module mgo.v2
go: to add module requirements and sums:
go mod tidy
PS D:\goprojects\GO_RESTAPI_MONGDB> go mod tidy
go: finding module for package gopkg.in/mgo.v2/bson
go: finding module for package gopkg.in/mgo.v2
go: found gopkg.in/mgo.v2 in gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
go: found gopkg.in/mgo.v2/bson in gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
go: finding module for package gopkg.in/yaml.v2
go: finding module for package gopkg.in/check.v1
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: found gopkg.in/check.v1 in gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: found gopkg.in/yaml.v2 in gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/kr/pretty v0.2.1
go: downloading github.com/kr/text v0.1.0
PS D:\goprojects\GO_RESTAPI_MONGDB>
PS D:\goprojects\GO_RESTAPI_MONGDB> ^C
PS D:\goprojects\GO_RESTAPI_MONGDB>
PS D:\goprojects\GO_RESTAPI_MONGDB>
PS D:\goprojects\GO_RESTAPI_MONGDB> go run main.go
Hello world
panic: no reachable servers
goroutine 1 [running]:
main.main()
D:/goprojects/GO_RESTAPI_MONGDB/main.go:27 +0x538
exit status 2
package main
import (
"fmt"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
)
type Movie struct {
Name string `bson:"name"`
Year string `bson:"year"`
Directors []string `bson:"directors"`
Writers []string `bson:"writers"`
BoxOffice `bson:"boxOffice"`
}
type BoxOffice struct {
Budget uint64 `bson:"budget"`
Gross uint64 `bson:"gross"`
}
func main() {
fmt.Println("Hello world")
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
c := session.DB("appDB").C("movidesData")
darkNight := &Movie{
Name: "Dark Night",
Year: "2009",
Directors: []string{"Chirstopher Nolan"},
Writers: []string{"Max Muler"},
BoxOffice: BoxOffice{
Budget: 150000,
Gross: 1750000000,
},
}
err = c.Insert(darkNight)
if err != nil {
log.Fatal(err)
}
result := Movie{}
err = c.Find(bson.M{"boxOffice.budget": bson.M{"$gt": 750000}}).One(&result)
//err = c.Find(bson.M{"boxOffice.budget": bson.M{"$gt": 150000000}}).One(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println("Movie Name is :", result.Name)
}
I know next to nothing about MongoDB, but maybe the following thoughts help.
The Mongo driver gopkg.in/mgo.v2 is unmaintained for 6 years. Even if you get past the connect problem, you might run into further issues.
What port is your local MongoDB listening on? If it is something else than 27017 (which appears to be the default port for MongoDB installations), youâd have to add the port number to the URL, e.g. mgo.Dial("localhost:27018")
@christophberger W hat is the right URL to follow to learn RESTAPI connection with MongoDB IN GOLANG? Any particular URL which is updated?
I am quite confused for the usage of go init module command and go.mod file and when to run go tidy etc.
I am following the below book, page : 131, topic: Introducing mgo, a MongoDB driver for Go
Somehow , independently mongdb runs in vscode, after adding PATH variable in systemâs variable.
But, I am stuck with the error at package main, basically how to build a go mod file? What are the steps to build a go mod file? This is a concern, when I am trying to use RESTAPI like gorilla mux.
I have stopped using Mongo driver gopkg.in/mgo.v2. And tried using mongo driver from official document but still not able to insert data to mongodb via REST API in golang. Below is the link where you can see the code and the screenshot.
Do you mind coming for a meeting for 5 mins may be. I can share my screen and show you what is happening. My email ID is : 2019hc04737@wilp.bits-pilani.ac.in, I can send a meeting invite, if you agree.
Try using 127.0.0.1 instead of localhost. Iâve seen this fix node access to mongodb recently.
Cheers - Andy
P.S. I assume youâve used mongosh to check that mongodb is running. I installed monogdb as a service in windows and that seemed simple.
If you havenât used mongosh before then just start it (from the command line) and it will fail if mongodb is running. Try âshow dbsâ will show the data stores which should show 3 as standard.
An easy way to check if mongodb is running (and remove that from the list of possible issues) is open http://127.0.0.1:27017 in a browser - you should see: It looks like you are trying to access MongoDB over HTTP on the native driver port.
I would try these next (you likely have already tried some):
Try a known working example piece of code - e.g. given with mongodb library. If this fails itâs likely an installation error - I would uninstall everything (go, mongodb, etc.), reinstall and try again.
If the known code works - then you needs to work forwards to your problem - i.e. add bits of it towards your solution until it fails.
You could also try running on a different machine/OS in case you have an unsupported OS (e.g. windows 32 bit).
Just another thought - are your username/password correct for accessing mongodb?