How to Query for Random Document from Mongo with Go
In this tutorial, I will show you how to query for a random document 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.
This code has NOT be included in the boilerplate. I used as the initial response to my home page suggestion response. We are going to add a new repository method, a new service method, a new handler method and add a route. Starting with the database layer and working towards the API, in
This call to the database using the Mongo driver grabs a "sample" of the data. You can see one of the options we are passing through is
maxNum. This represents the maximum number of elements you want to sample. We use
ListCarQuery as a filter. This allows us to continue to filter by model, make, year, status and email. This method does not use the page and limit attributes.
Next, we will add the service. This will be very similar to
GetAll except it returns only one element. In
GetRandom. Again, it's very similar to the
GetAll. This is for calling the
Lastly, we need to add a new endpoint to call the handler. Since we have structured an endpoint to be
/cars/:id. We cannot use
/cars/random or any other extension that doesn't require an ID. We will add a whole new one. I don't know if I completely agree with this but I will leave it up to your API design decision.
Now, let's test this all out. First verify you have data in your database to pull from.
You will then need to sign up or sign into an account. We want to get a
token from the response for authentication purposes. My request looks like:
Or the curl command:
I capture the token. I'm going to use it with my next request. We add a
GET request at
/special/cars/random. It should return a single random car. My request in POSTman:
Or the curl command:
I'm going to make the request again to confirm that the randomize feature works.
As you can see I got back a different car. Thanks for reading!