Go Lang and MongoDB Web App MVC pattern Part 1
Introduction
This is part one in a multi-part tutorial series providing instructions for creating a Go Lang and MongoDB Web App with MVC pattern. This series will provide step-by-step instructions for creating a Golang PostgreSQL web app with a MVC pattern and part one will cover setting up the project development environment and creating a sample database in MongoDB.
Prerequisite
- MongoDB and Golang must both be properly installed and configured.
Execute the following command, in the terminal, to confirm what version of Golang is installed.
1 | go version |
The output should resemble the following:
1 | go version go1.13.4 windows/amd64 |
The Go Lang Software Setting
- To ensure the application will work properly, confirm the Golang setting is properly configured before beginning creating the Golang and MongoDB web app with MVC pattern. Execute the following command in the terminal to check the current configuration:
1 | go env |
The output should resemble the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | set GO111MODULE= set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\user\AppData\Local\go-build set GOENV=C:\Users\user\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=C:\Projects\Go set GOPRIVATE= set GOPROXY=https://proxy.golang.org,direct set GOROOT=c:\go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=c:\go\pkg\tool\windows_amd64 |
NOTE: Because the results can vary, depending on the system that was configured, the configuration process is explained here in detail:
- Configure the
GOPATH
as shown above, bearing in mind that to keep the same GOPATH configuration it must be set up with the system environment variables as shown in the following image:
-img src=”https://i.gyazo.com/28173c4a75b55c5d67ff4123b97ebcb3.png” width=”632″ height=”456″-
Creating the Project Directory
With the Golang project configured using the proper system environment variables, this section will breakdown the application’s project structure:
- Ecommerce: This is name of the project directory.
- Config: The config folder will hold the Go files that were used in the configuration and database connection process.
- Model: This directory will handle the files that pertain to the logic of the web application.
- Templates: The templates directory will handle all the templates for the various pages required by the web app.
Creating the MongoDB Sample Dataset
This section will explain how to create the sample dataset for the application using the Mongo shell.
First, execute the following command to connect to Mongo:
1 | mongo |
Once connected to the MongoDB server, execute the use EcommerceDb
command to connect to the desired database.
NOTE: Even though it does not yet exist, this database will be automatically created once a document is successfully inserted.
Execute the following code to insert a sample dataset:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | db.product.insertMany([ { "sku" : "111445GB3", "title" : "Trident Mobile One", "description" : "The future of Mobile", "qty" : 80, "pricing" : 150 }, { "sku" : "111445GF5", "title" : "Samyong Mobile One", "description" : "The fastest Mobile Phone", "qty" : 80, "pricing" : 150 } ]); |
The output should resemble the following:
1 2 3 4 5 6 7 | { "acknowledged" : true, "insertedIds" : [ ObjectId("5df09cd00b8042214e6ffab2"), ObjectId("5df09cd00b8042214e6ffab3") ] } |
To verify the database was successfully created, execute the following show dbs
command:
1 2 3 4 | > show dbs EcommerceDb 0.000GB EmployeeDb 0.000GB ProductDb 0.000GB |
Now, to confirm the documents were successfully created, execute the following command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | > db.product.find().pretty() { "_id" : ObjectId("5df09cd00b8042214e6ffab2"), "sku" : "111445GB3", "title" : "Trident Mobile One", "description" : "The future of Mobile", "qty" : 80, "pricing" : 150 } { "_id" : ObjectId("5df09cd00b8042214e6ffab3"), "sku" : "111445GF5", "title" : "Samyong Mobile One", "description" : "The fastest Mobile Phone", "qty" : 80, "pricing" : 150 } > |
Following is a breakdown of the above code:
- The method
find()
is used to locate all of the documents within theproduct
collections. - The method
pretty()
is used to format the results in a more readable manner. - As discussed, the documents were successfully inserted with the method
insertMany()
.
Conclusion
This was part one in a multi-part tutorial series providing instructions for creating a Go Lang and MongoDB Web App with MVC pattern. Part one of this series covered the required prerequisites and the Golang software settings. Part one also covered how to creating the project directory and the MongoDB sample dataset. Remember that the database will be automatically created once a document is successfully inserted after connecting to the MongoDB server. Part two in this tutorial series will go into detail explaining more of the functions needed to create a Go Lang and MongoDB Web App with MVC pattern. Jump to top
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started