Lessons

How to Insert Data into Mongo with Go

In this tutorial, I will show you the basics of doing an insert into a Mongo database instance with Go code. I will be on local and provide you with all steps.

Setup

I'm assuming you have Mongo installed on your local machine. I also recommend installing Mongo Compass to make it easier to see your data.

I am assuming you have Go installed and have successful compiled at least one Go file. This would confirm your environment works. We will be using my Go boierplate code to save time on setting up our code structure. It follows a handlers, models, repositories, services, and utils structure.

In short:

  • handlers handles the inbound request.
  • models contains all structs that represent data.
  • repositories has all methods for interacting with the database.
  • services contains the business logic of the app.
  • utils has any helper methods.

Download the Go boilerplate. Please give it a star on Github so I know people are still using and enjoying it. Move the files into your new project root. Open the files up in a text editor.

You need to do one thing for this boilerplate to work. Open services/user_service.go and within the IsValidPassword function. You will need to switch the variable to true. Do NOT use this code in production without fixing it. I leave encryption and password requirements up to you. This will store a plain text password till you add code to GetEncryptedPassword and IsValidPassword. For demo and dev purposes, this is fine.

Insert Code

As you can imagine, we will be working with the repositories layer since it works with the database. If we open the repositories/cars_repository.go file, you can see the Save method.

1 2 3 4 5 6 7 8 func (c *CarsRepository) Save(car models.Car) error { _, err := c.db.Collection("cars").InsertOne(context.TODO(), car) if err != nil { return err } // insertResult.InsertedID.(string) return nil }

This saves the models.Car object into a cars collection. You can capture the new document id by changing it to be:

1 2 3 4 5 6 7 func (c *CarsRepository) Save(car models.Car) (string, error) { insertResult, err := c.db.Collection("cars").InsertOne(context.TODO(), car) if err != nil { return "", err } return insertResult.InsertedID.(string), nil }

For the models.Car struct, you can find it in models/cars.go.

1 2 3 4 5 6 7 8 9 type Car struct { ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` Make string `json:"make" bson:"make"` Model string `json:"model" bson:"model"` Year int `json:"year" bson:"year"` Status string `json:"status" bson:"status"` Email string `json:"email" bson:"email"` Created time.Time `json:"created" bson:"created"` }

Each field has already been defined with the bson tag.

Demo

In your terminal:

1 go run main.go

You will need to create a user first. This POSTman request:

Screenshot of Github Workflow Running

This curl request does the same thing:

1 2 3 4 5 6 curl --location --request POST 'http://localhost:8080/user/signup/' \ --header 'Content-Type: application/json' \ --data-raw '{ "email": "me@keithweaver.ca", "password": "demodemo1" }'

Copy the token in the response somewhere.

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

As you can see, we have successfully written to Mongo. Let's try to run the Save code we just created. You can do it with this POSTman request:

Screenshot of Github Workflow Running

This curl command does the same thing.

1 2 3 4 5 6 7 8 curl --location --request POST 'http://localhost:8080/cars/' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer <YOUR_TOKEN_FROM_SIGN_UP_RESPONSE>' \ --data-raw '{ "make": "Ferrari", "model": "Enzo", "year": 1994 }'

If you navigate back to Mongo Compass, you may not see your new data. You may have to hit CMD + R or View > Reload.

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

As you can see, we have created our new object and saved it to Mongo.

Thanks for reading!