am attempting to connect with MongoDB by way of Go lang REST API. Making an attempt to insert a film. However, the connection to MongoDB is failing. The Golang REST API growth states to write down 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 operating on home windows to golang operating 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/examine.v1
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading gopkg.in/examine.v1 v1.0.0-20201130134442-10cb98267c6c
go: discovered gopkg.in/examine.v1 in gopkg.in/examine.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
Hiya 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 {
Title string `bson:"title"`
Yr string `bson:"yr"`
Administrators []string `bson:"administrators"`
Writers []string `bson:"writers"`
BoxOffice `bson:"boxOffice"`
}
sort BoxOffice struct {
Price range uint64 `bson:"funds"`
Gross uint64 `bson:"gross"`
}
func foremost() {
fmt.Println("Hiya world")
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Shut()
c := session.DB("appDB").C("movidesData")
darkNight := &Film{
Title: "Darkish Evening",
Yr: "2009",
Administrators: []string{"Chirstopher Nolan"},
Writers: []string{"Max Muler"},
BoxOffice: BoxOffice{
Price range: 150000,
Gross: 1750000000,
},
}
err = c.Insert(darkNight)
if err != nil {
log.Deadly(err)
}
consequence := Film{}
err = c.Discover(bson.M{"boxOffice.funds": bson.M{"$gt": 750000}}).One(&consequence)
//err = c.Discover(bson.M{"boxOffice.funds": bson.M{"$gt": 150000000}}).One(&consequence)
if err != nil {
log.Deadly(err)
}
fmt.Println("Film Title is :", consequence.Title)
}
@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.
-
The Mongo driver
gopkg.in/mgo.v2
is unmaintained for six years. Even if you happen to get previous the join downside, you may run into additional points. -
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 best URL to comply with to study RESTAPI reference to MongoDB IN GOLANG? Any explicit 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 under e book, web page : 131, subject: Introducing mgo, a MongoDB driver for Go
@christophberger In VSCODE, I checked the mongoDB connects on the similar port : it states “mongodb related at
@christophberger I’m dealing with under error with bundle foremost. What’s the answer for a similar?
@christophberger The under is how my atmosphere seems like:
I can not see any apparent downside with the Dial name.
What I’d do in such a scenario: I’d strive the official MongoDB driver (docs right here).
If the official driver connects efficiently, I’d change over.
If the official driver returns the identical error, the issue is exterior the code. Possibly some bizarre native firewall setting?
1 Like
In some way , 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 capable of insert information 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 occurring. My e-mail ID is : 2019hc04737@wilp.bits-pilani.ac.in, I can ship a gathering invite, if you happen to 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 examine that mongodb is operating. I put in monogdb as a service in home windows and that appeared easy.
When you haven’t used mongosh earlier than then simply begin it (from the command line) and it’ll fail if mongodb is operating. Strive ‘present dbs’ will present the info shops which ought to present 3 as commonplace.
1 Like
Sorry – I meant “if mongodb isn’t operating” it should error – can’t appear to edit it now…
A simple technique to examine if mongodb is operating (and take away that from the record of attainable points) is open http://127.0.0.1:27017 in a browser – it’s best to see:It seems like you are attempting to entry MongoDB over HTTP on the native driver port.
@Andrew_Stratton MongoDB server is up and operating. I’ve checked it in Home windows–>companies and such as you mentioned.
@Andrew_Stratton even when I sort localhost, it reveals the identical consequence.
No downside – simply checking that the ‘tank isn’t empty’.
I discover 127.0.0.1 safer to make use of than localhost as a result of
- localhost features a lookup and may fail when 127.0.0.1 doesn’t
- localhost will be redirected within the hosts file to a unique ip handle
I’d strive these subsequent (you probably have already tried some):
- Strive a identified working instance piece of code – e.g. given with mongodb library. If this fails it’s probably an set up error – I’d uninstall all the pieces (go, mongodb, and so on.), reinstall and check out once more.
- If the identified code works – you then must work forwards to your downside – i.e. add bits of it in direction of your answer till it fails.
- You may additionally strive operating on a unique machine/OS in case you might have an unsupported OS (e.g. home windows 32 bit).
- Simply one other thought – are your username/password appropriate for accessing mongodb?
Finest needs – Andy