Basic Usage
Use existing MongoDB connection, or setup a new one to register a default database connection.
For existing database connection,
import "github.com/Lyearn/mgod"
func init() {
// client is the MongoDB client obtained using Go Mongo Driver's Connect method.
mgod.SetDefaultClient(client)
}
To setup a new connection,
import (
"time"
"github.com/Lyearn/mgod"
"go.mongodb.org/mongo-driver/mongo/options"
)
func init() {
// `cfg` is optional. Can rely on default configurations by providing `nil` value in argument.
cfg := &mgod.ConnectionConfig{Timeout: 5 * time.Second}
opts := options.Client().ApplyURI("mongodb://root:mgod123@localhost:27017")
err := mgod.ConfigureDefaultClient(cfg, opts)
}
note
The above err
variable will be a connection error (if occurs) returned by the Go Mongo Driver. So, handle the error accordingly.
Add tags (wherever applicable) in existing struct (or define a new model).
type User struct {
Name string
EmailID string `bson:"emailId"`
Age *int32 `bson:",omitempty"`
JoinedOn string `bson:"joinedOn" mgoType:"date"`
}
Use mgod
to get the entity ODM.
import (
"github.com/Lyearn/mgod"
"github.com/Lyearn/mgod/schema/schemaopt"
)
model := User{}
dbName := "mgoddb"
collection := "users"
schemaOpts := schemaopt.SchemaOptions{
Timestamps: true,
}
opts := mgod.NewEntityMongoModelOptions(dbName, collection, &schemaOpts)
userModel, _ := mgod.NewEntityMongoModel(model, *opts)
Use the entity ODM to perform CRUD operations with ease.
Inserting a new document
joinedOn, _ := dateformatter.New(time.Now()).GetISOString()
userDoc := User{
Name: "Gopher",
EmailID: "gopher@mgod.com",
JoinedOn: joinedOn,
}
user, _ := userModel.InsertOne(context.TODO(), userDoc)
Output:
{
"_id": ObjectId("65697705d4cbed00e8aba717"),
"name": "Gopher",
"emailId": "gopher@mgod.com",
"joinedOn": ISODate("2023-12-01T11:32:19.290Z"),
"createdAt": ISODate("2023-12-01T11:32:19.290Z"),
"updatedAt": ISODate("2023-12-01T11:32:19.290Z"),
"__v": 0
}
Notice how _id
, createdAt
, updatedAt
and __v
fields are added automatically.
Finding documents using model properties
users, _ := userModel.Find(context.TODO(), bson.M{"name": userDoc.Name})
Output:
[]User{
User{
Name: "Gopher",
EmailID: "gopher@mgod.com",
JoinedOn: "2023-12-01T11:32:19.290Z",
}
}
Updating document properties
result, _ := userModel.UpdateMany(context.TODO(), bson.M{"joinedOn": bson.M{"$gte": "2023-12-01T00:00:00.000Z"}}, bson.M{"$inc": {"__v": 1}})
Output:
mongo.UpdateResult{
MatchedCount: 1,
ModifiedCount: 1,
UpsertedCount: 0,
}
// User Doc
{
"_id": ObjectId("65697705d4cbed00e8aba717"),
"name": "Gopher",
"emailId": "gopher@mgod.com",
"joinedOn": ISODate("2023-12-01T11:32:19.290Z"),
"createdAt": ISODate("2023-12-01T11:32:19.290Z"),
"updatedAt": ISODate("2023-12-02T10:40:00.670Z"),
"__v": 1
}
Notice the updation of the updatedAt
field.
Removing documents matching certain or all model properties
result, _ := userModel.DeleteMany(context.TODO(), bson.M{"name": userDoc.Name})
Output:
mongo.DeleteResult{
DeletedCount: 1
}