How to Query (Multiple Docs) Data from Mongo with Go
In this tutorial, I will show you the basics of doing a query for multiple documents in a Mongo database instance with Go code. I will be on local and provide you with all steps.
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
handlershandles the inbound request.
modelscontains all structs that represent data.
repositorieshas all methods for interacting with the database.
servicescontains the business logic of the app.
utilshas 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
IsValidPassword. For demo and dev purposes, this is fine.
You will need data in your database and checkout the insert tutorial.
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
In the code above, we build a filter using the query parameters. We set the options for paging. Next, we sort by a column. In this case, we used most recently created. We then query for the cars and build a slice of them to be returned. The only issue with this method is the
err within the loop. If a car cannot be decoded, it is simply excluded. You can choose your own way of handling the error. Whether it would be building another list, grabbing an id for an error list, etc.
Let's take a closer look at the query attribute being passed in. In
models/cars.go you can find
It takes a few search attributes. If you look at the
Filter method, it uses Regex. I explain how to use regex and Go in another lesson. You can find out how
year values are used in that lesson. The two main attributes we are focusing on are
Limit is the maximum number of elements someone is allowed to show. This helps limit long living queries, and taking too much of the database resources.
Page allow us to iterate through sets of results. This is why we need to sort or results in the
limit are not provided? How do we set them? You can look in
handlers/car_handler.go and at
GetAll. Right at the top of the method, we grab the query parameters.
We then convert them from being strings to being integers. This stops the requests that could be
?limit=foobar. We then build our model object with all the set values. This block of code does lack a few extra checks. We should be checking to see if page is greater than 0. We should be verifying that limit is within some bounds (Ex. 1 to 30). Technically, a user can go
/cars/?page=1&limit=10000000 and pull the entire database. Right above the code to setup the new
ListCarQuery, you can add:
When you do not provide a
year. The only attribute in the filter is
In your terminal:
The first step is to run sign up or sign in. I execute my sign up request with Postman. Copy down the
token in the response.
Or you can run this curl command:
Write down the token. Connect to your database and confirm that you have multiple values.
Next, you will submit a GET request.
Or the curl command:
And the response returns a list of cars:
Here is a screenshot of my request:
Thanks for reading!