Skip to content

Mongo trace is a simple tool who help you to track and save operations performed on your Mongo Database.

Notifications You must be signed in to change notification settings

DipandaAser/mongotrace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongotrace

Mongo trace is a simple tool who help you to track and save operations performed on your Mongo Database.

Usage

This is just a quick introduction.

Let's start with a trivial example:

package main

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
	"log"
)

type Person struct {
	ID   string `json:"_id" bson:"_id"`
	Name string `json:"name" bson:"name"`
}

func main() {
	ctx := context.TODO()
	clientOptions := options.Client().ApplyURI("mongodb:https://localhost:27017")
	client, err := mongo.Connect(ctx, clientOptions)
	if err != nil {
		log.Fatal(err)
	}
	err = client.Ping(ctx, readpref.Primary())
	if err != nil {
		log.Fatal(err)
	}

	db := client.Database("Test")

	// Create our first record
	initialRecord := Person{
		ID:   "1234567890",
		Name: "Jane",
	}
	_, err = db.Collection("Person").InsertOne(ctx, initialRecord)
	if err != nil {
		log.Fatal(err)
	}
	
	// Now we update our first record
	filter := bson.M{"_id": initialRecord.ID}
	_, err = db.Collection("Person").UpdateOne(ctx, filter, bson.M{"$set": bson.M{"name":"Jane Doe"}})
	if err != nil {
		log.Fatal(err)
	}

	trace, err := TraceOperationUpdateWithFilter(db, "Trace", "Person", initialRecord, filter)
	if err != nil {
		log.Fatal(err)
	}
	
	
	fmt.Println(trace.Difference)
}

This output in a html "pre" tag will be will be

alt text

Avialable func

mongotrace.TraceOperationAdd()
mongotrace.TraceOperationDelete()
mongotrace.TraceOperationUpdate()
mongotrace.TraceOperationUpdateWithFilter()

About

Mongo trace is a simple tool who help you to track and save operations performed on your Mongo Database.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages