This repository demonstrates how to set up a Go project to perform CRUD (Create, Read, Update, Delete) operations using MongoDB.
- MongoDB connection and operations in Go.
- CRUD operations.
- Go 1.15 or higher
- MongoDB
- Fiber Go Web Framework
-
Clone the repository:
git clone https://github.com/muthukumar89uk/go-with-mongodb.git
Click here to directly download it.
-
Install Go dependencies:
go mod tidy
-
Locally:
Install MongoDB from the official MongoDB website.
-
Create the
Employee
struct:Create a
models
directory and anmodels.go
file with the following content:type Employee struct { Id string `json:"id,omitempty" bson:"_id,omitempty"` Name string `json:"name,omitempty" bson:"name,omitempty"` Age int `json:"age,omitempty" bson:"age,omitempty"` }
-
Run the Go application:
go run .
-
API Endpoints:
- Create an employee:
POST /create
- Get an employee by ID:
GET /getemployee
- Update an employee by ID:
PUT /update-employees/:id
- Delete an employee by ID:
DELETE /delete-employees/:id
- Create an employee: