MongoDB: Connect
Learn how to connect to a MongoDB database.
We'll cover the following...
With our generic interface in place, it’s time to put all our theory into practice and see how to connect to MongoDB. For the purposes of this course, we will use MongoDB as our database of choice for experimenting and learning about interacting with databases.
Set up a struct
For this purpose, we will first set up a mongo
package and then we will create a struct that will implement our DB
interface.
package mongoimport (// ... imports here ...)type Mongo struct {Client *mongo.Client}
The Mongo struct
The struct will only have a Client
member of the *mongo.Client
type, which is the client of the official MongoDB driver for Go. This client will be used for any and every operation in our application.