Connection from GOlang RESTAPI to mongodb failing in VSCODE

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 , I tried writing both localhost and 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
go: finding module for package
go: found in v2.0.0-20190816093944-a6b53ec6cb22
go: found in v2.0.0-20190816093944-a6b53ec6cb22
go: finding module for package
go: finding module for package
go: downloading v2.4.0
go: downloading v1.0.0-20201130134442-10cb98267c6c
go: found in v1.0.0-20201130134442-10cb98267c6c
go: found in v2.4.0
go: downloading v0.2.1
go: downloading v0.1.0
PS D:\goprojects\GO_RESTAPI_MONGDB> ^C
PS D:\goprojects\GO_RESTAPI_MONGDB> go run main.go
Hello world
panic: no reachable servers

goroutine 1 [running]:
        D:/goprojects/GO_RESTAPI_MONGDB/main.go:27 +0x538
exit status 2
package main

import (
    mgo ""

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 {
    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 {
    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 {
    fmt.Println("Movie Name is :", result.Name)

@Metalymph Can you please help with this program, I ran this on VSCODE

Hi @shubhra,

I know next to nothing about MongoDB, but maybe the following thoughts help.

  1. The Mongo driver is unmaintained for 6 years. Even if you get past the connect problem, you might run into further issues.

  2. 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")

1 Like

@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

@christophberger In VSCODE, I checked the mongoDB connects at the same port : it states "mongodb connected at

@christophberger I am facing below error with package main. What is the solution for the same?

@christophberger The below is how my environment looks like:

I cannot see any obvious problem with the Dial call.

What I would do in such a situation: I’d try the official MongoDB driver (docs here).

If the official driver connects successfully, I would switch over.

If the official driver returns the same error, the problem is outside the code. Maybe some weird local firewall setting?

1 Like

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 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 :, I can send a meeting invite, if you agree.

Try using 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.

1 Like

Sorry - I meant “if mongodb isn’t running” it will error - can’t seem to edit it now…

An easy way to check if mongodb is running (and remove that from the list of possible issues) is open in a browser - you should see:
It looks like you are trying to access MongoDB over HTTP on the native driver port.

@Andrew_Stratton MongoDB server is up and running. I have checked it in Windows–>services and like you said.

@Andrew_Stratton even if I type localhost, it shows the same result.

No problem - just checking that the ‘tank isn’t empty’.

I find safer to use than localhost because

  • localhost includes a lookup and can fail when doesn’t
  • localhost can be redirected in the hosts file to a different ip address

I would try these next (you likely have already tried some):

  1. 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.
  2. 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.
  3. You could also try running on a different machine/OS in case you have an unsupported OS (e.g. windows 32 bit).
  4. Just another thought - are your username/password correct for accessing mongodb?

Best wishes - Andy

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.