Friday, November 14, 2025
HomeGolangConnection from GOlang RESTAPI to mongodb failing in VSCODE - Getting Assist

Connection from GOlang RESTAPI to mongodb failing in VSCODE – Getting Assist


am attempting to connect with MongoDB by way of Go lang REST API. Attempting to insert a film. However, the connection to MongoDB is failing. The Golang REST API increase states to put in writing 127.0.0.1 , I attempted writing each localhost and 127.0.0.1 however nonetheless it fails, I imply in VSCODE how one can join mongodb, which is working on home windows to golang working in VSCODE. I put in all mongodb extensions in VSCODE.

Error I'm dealing with on VSCODE:

PS D:goprojectsGO_RESTAPI_MONGDB> go mod init mgo.v2 
go: creating new go.mod: module mgo.v2
go: so as to add module necessities and sums:
        go mod tidy
PS D:goprojectsGO_RESTAPI_MONGDB> go mod tidy
go: discovering module for bundle gopkg.in/mgo.v2/bson
go: discovering module for bundle gopkg.in/mgo.v2
go: discovered gopkg.in/mgo.v2 in gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
go: discovered gopkg.in/mgo.v2/bson in gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
go: discovering module for bundle gopkg.in/yaml.v2
go: discovering module for bundle gopkg.in/verify.v1
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading gopkg.in/verify.v1 v1.0.0-20201130134442-10cb98267c6c
go: discovered gopkg.in/verify.v1 in gopkg.in/verify.v1 v1.0.0-20201130134442-10cb98267c6c
go: discovered gopkg.in/yaml.v2 in gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/kr/fairly v0.2.1
go: downloading github.com/kr/textual content v0.1.0
PS D:goprojectsGO_RESTAPI_MONGDB>
PS D:goprojectsGO_RESTAPI_MONGDB> ^C
PS D:goprojectsGO_RESTAPI_MONGDB>
PS D:goprojectsGO_RESTAPI_MONGDB>
PS D:goprojectsGO_RESTAPI_MONGDB> go run foremost.go
Whats up world
panic: no reachable servers

goroutine 1 [running]:
foremost.foremost()
        D:/goprojects/GO_RESTAPI_MONGDB/foremost.go:27 +0x538
exit standing 2
bundle foremost

import (
    "fmt"
    mgo "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "log"
)

sort Film struct {
    Identify      string   `bson:"identify"`
    Yr      string   `bson:"12 months"`
    Administrators []string `bson:"administrators"`
    Writers   []string `bson:"writers"`
    BoxOffice `bson:"boxOffice"`
}

sort BoxOffice struct {
    Finances uint64 `bson:"finances"`
    Gross  uint64 `bson:"gross"`
}

func foremost() {
    fmt.Println("Whats up world")
    session, err := mgo.Dial("localhost")
    if err != nil {
        panic(err)
    }
    defer session.Shut()
    c := session.DB("appDB").C("movidesData")
    darkNight := &Film{
        Identify:      "Darkish Night time",
        Yr:      "2009",
        Administrators: []string{"Chirstopher Nolan"},
        Writers:   []string{"Max Muler"},
        BoxOffice: BoxOffice{
            Finances: 150000,
            Gross:  1750000000,
        },
    }
    err = c.Insert(darkNight)
    if err != nil {
        log.Deadly(err)
    }
    end result := Film{}
    err = c.Discover(bson.M{"boxOffice.finances": bson.M{"$gt": 750000}}).One(&end result)
    //err = c.Discover(bson.M{"boxOffice.finances": bson.M{"$gt": 150000000}}).One(&end result)
    if err != nil {
        log.Deadly(err)
    }
    fmt.Println("Film Identify is :", end result.Identify)
}

@Metalymph Are you able to please assist with this program, I ran this on VSCODE

Hello @shubhra,

I do know subsequent to nothing about MongoDB, however perhaps the next ideas assist.

  1. The Mongo driver gopkg.in/mgo.v2 is unmaintained for six years. Even should you get previous the join drawback, you may run into additional points.

  2. What port is your native MongoDB listening on? Whether it is one thing else than 27017 (which seems to be the default port for MongoDB installations), you’d have so as to add the port quantity to the URL, e.g. mgo.Dial("localhost:27018")

1 Like

@christophberger W hat is the correct URL to comply with to be taught RESTAPI reference to MongoDB IN GOLANG? Any specific URL which is up to date?
I’m fairly confused for the utilization of go init module command and go.mod file and when to run go tidy and so on.
I’m following the beneath e book, web page : 131, subject: Introducing mgo, a MongoDB driver for Go

@christophberger In VSCODE, I checked the mongoDB connects on the identical port : it states “mongodb linked at

@christophberger I’m dealing with beneath error with bundle foremost. What’s the answer for a similar?

@christophberger The beneath is how my setting appears like:

I can not see any apparent drawback with the Dial name.

What I’d do in such a state of affairs: I’d strive the official MongoDB driver (docs right here).

If the official driver connects efficiently, I’d swap over.

If the official driver returns the identical error, the issue is exterior the code. Possibly some bizarre native firewall setting?

1 Like

One way or the other , independently mongdb runs in vscode, after including PATH variable in system’s variable.
However, I’m caught with the error at bundle foremost, principally how one can construct a go mod file? What are the steps to construct a go mod file? It is a concern, when I’m attempting to make use of RESTAPI like gorilla mux.
I’ve stopped utilizing Mongo driver gopkg.in/mgo.v2. And tried utilizing mongo driver from official doc however nonetheless not in a position to insert knowledge to mongodb by way of REST API in golang. Under is the hyperlink the place you may see the code and the screenshot.
Do you thoughts coming for a gathering for five minutes could also be. I can share my display and present you what is going on. My e mail ID is : 2019hc04737@wilp.bits-pilani.ac.in, I can ship a gathering invite, should you agree.

Strive utilizing 127.0.0.1 as a substitute of localhost. I’ve seen this repair node entry to mongodb just lately.

Cheers – Andy

P.S. I assume you’ve used mongosh to verify that mongodb is working. I put in monogdb as a service in home windows and that appeared easy.

In case you haven’t used mongosh earlier than then simply begin it (from the command line) and it’ll fail if mongodb is working. Strive ‘present dbs’ will present the information shops which ought to present 3 as customary.

1 Like

Sorry – I meant “if mongodb isn’t working” it should error – can’t appear to edit it now…

A simple solution to verify if mongodb is working (and take away that from the listing of potential points) is open http://127.0.0.1:27017 in a browser – it’s best to see:
It appears like you are attempting to entry MongoDB over HTTP on the native driver port.

@Andrew_Stratton MongoDB server is up and working. I’ve checked it in Home windows–>providers and such as you mentioned.

@Andrew_Stratton even when I sort localhost, it exhibits the identical end result.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments