Skip to content

Instantly share code, notes, and snippets.

@radhakishans1378
Last active May 4, 2024 16:38
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save radhakishans1378/852fd10a286a03bd76ffcad00aec417b to your computer and use it in GitHub Desktop.
Save radhakishans1378/852fd10a286a03bd76ffcad00aec417b to your computer and use it in GitHub Desktop.
mongo-crud
package main
import (
"context"
"go-preparation/go-frameworks/go-mongo-crud/connectionhelper"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"gopkg.in/gookit/color.v1"
)
//Issue - struct to map with mongodb documents
type Issue struct {
ID primitive.ObjectID `bson:"_id"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
Title string `bson:"title"`
Code string `bson:"code"`
Description string `bson:"description"`
Completed bool `bson:"completed"`
}
//CreateIssue - Insert a new document in the collection.
func CreateIssue(task Issue) error {
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return err
}
//Create a handle to the respective collection in the database.
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform InsertOne operation & validate against the error.
_, err = collection.InsertOne(context.TODO(), task)
if err != nil {
return err
}
//Return success without any error.
return nil
}
//CreateMany - Insert multiple documents at once in the collection.
func CreateMany(list []Issue) error {
//Map struct slice to interface slice as InsertMany accepts interface slice as parameter
insertableList := make([]interface{}, len(list))
for i, v := range list {
insertableList[i] = v
}
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return err
}
//Create a handle to the respective collection in the database.
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform InsertMany operation & validate against the error.
_, err = collection.InsertMany(context.TODO(), insertableList)
if err != nil {
return err
}
//Return success without any error.
return nil
}
//GetIssuesByCode - Get All issues for collection
func GetIssuesByCode(code string) (Issue, error) {
result := Issue{}
//Define filter query for fetching specific document from collection
filter := bson.D{primitive.E{Key: "code", Value: code}}
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return result, err
}
//Create a handle to the respective collection in the database.
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform FindOne operation & validate against the error.
err = collection.FindOne(context.TODO(), filter).Decode(&result)
if err != nil {
return result, err
}
//Return result without any error.
return result, nil
}
//GetAllIssues - Get All issues for collection
func GetAllIssues() ([]Issue, error) {
//Define filter query for fetching specific document from collection
filter := bson.D{{}} //bson.D{{}} specifies 'all documents'
var issues []Issue
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return issues, err
}
//Create a handle to the respective collection in the database.
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform Find operation & validate against the error.
cur, findError := collection.Find(context.TODO(), filter)
if findError != nil {
return issues, findError
}
//Map result to slice
for cur.Next(context.TODO()) {
var t Issue
err := cur.Decode(&t)
if err != nil {
return issues, err
}
issues = append(issues, t)
}
// once exhausted, close the cursor
cur.Close(context.TODO())
if len(issues) == 0 {
return issues, mongo.ErrNoDocuments
}
return issues, nil
}
//DeleteOne - Get All issues for collection
func DeleteOne(code string) error {
//Define filter query for fetching specific document from collection
filter := bson.D{primitive.E{Key: "code", Value: code}}
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return err
}
//Create a handle to the respective collection in the database.
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform DeleteOne operation & validate against the error.
_, err = collection.DeleteOne(context.TODO(), filter)
if err != nil {
return err
}
//Return success without any error.
return nil
}
//DeleteAll - Get All issues for collection
func DeleteAll() error {
//Define filter query for fetching specific document from collection
selector := bson.D{{}} // bson.D{{}} specifies 'all documents'
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return err
}
//Create a handle to the respective collection in the database.
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform DeleteMany operation & validate against the error.
_, err = collection.DeleteMany(context.TODO(), selector)
if err != nil {
return err
}
//Return success without any error.
return nil
}
//PrintList - Print list of issues on console
func PrintList(issues []Issue) {
for i, v := range issues {
if v.Completed {
color.Green.Printf("%d: %s %s\n", i+1, v.Code, v.Title)
} else {
color.Yellow.Printf("%d: %s %s\n", i+1, v.Code, v.Title)
}
}
}
// MarkCompleted - MarkCompleted
func MarkCompleted(code string) error {
//Define filter query for fetching specific document from collection
filter := bson.D{primitive.E{Key: "code", Value: code}}
//Define updater for to specifiy change to be updated.
updater := bson.D{primitive.E{Key: "$set", Value: bson.D{
primitive.E{Key: "completed", Value: true},
}}}
//Get MongoDB connection using connectionhelper.
client, err := connectionhelper.GetMongoClient()
if err != nil {
return err
}
collection := client.Database(connectionhelper.DB).Collection(connectionhelper.ISSUES)
//Perform UpdateOne operation & validate against the error.
_, err = collection.UpdateOne(context.TODO(), filter, updater)
if err != nil {
return err
}
//Return success without any error.
return nil
}
func main() {
/* newIssue := Issue{
ID: primitive.NewObjectID(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Title: "This is issue 3",
Description: "This is issue 3",
Code: "I0003",
Completed: false,
}
// CreateIssue(newIssue) */
// DeleteOne("I0001")
DeleteAll()
issues, _ := GetAllIssues()
PrintList(issues)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment