Documentation ¶
Overview ¶
mongo is a simple wrapper for MongoDb Driver, this package uses "id" instead of "_id" to find or add a document.
It is important to know that you will have to index id field for optimum performance.
In general you would't need this package at all, if you rely more on "id" and simple access to MongoDB API then this module will help you.
Example:
import "github.com/akshaybabloo/mongo" type data struct { Id int `bson:"id"` Name string `bson:"name"` } func main() { client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } testData := data{ Id: 1, Name: "Akshay", } done, err := client.Add("test_collection", testData) if err != nil { panic(err) } print(done.InsertedID) }
Index ¶
- type MongoDb
- type NewMongoDbClient
- func (connectionDetails NewMongoDbClient) Add(collectionName string, data interface{}) (*mongo.InsertOneResult, error)
- func (connectionDetails NewMongoDbClient) AddMany(collectionName string, data []interface{}) (*mongo.InsertManyResult, error)
- func (connectionDetails NewMongoDbClient) Collection(collectionName string) (*mongo.Collection, *mongo.Client, context.Context)
- func (connectionDetails NewMongoDbClient) DB() *mongo.Database
- func (connectionDetails NewMongoDbClient) Delete(collectionName string, id string) (*mongo.DeleteResult, error)
- func (connectionDetails NewMongoDbClient) Get(collectionName string, id string) *mongo.SingleResult
- func (connectionDetails NewMongoDbClient) GetAll(collectionName string, id string, result interface{}) error
- func (connectionDetails NewMongoDbClient) GetAllCustom(collectionName string, filter interface{}, result interface{}) error
- func (connectionDetails NewMongoDbClient) GetCustom(collectionName string, filter interface{}) *mongo.SingleResult
- func (connectionDetails NewMongoDbClient) Update(collectionName string, id string, data interface{}) (*mongo.UpdateResult, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MongoDb ¶
type MongoDb interface { Add(collectionName string, data interface{}) (*mongo.InsertOneResult, error) AddMany(collectionName string, data []interface{}) (*mongo.InsertManyResult, error) Update(collectionName string, id string, data interface{}) (*mongo.UpdateResult, error) Delete(collectionName string, id string) (*mongo.DeleteResult, error) Get(collectionName string, id string) *mongo.SingleResult GetCustom(collectionName string, id string) *mongo.SingleResult GetAll(collectionName string, id string, result interface{}) error GetAllCustom(collectionName string, id string, result interface{}) error Collection(collectionName string) (*mongo.Collection, *mongo.Client, context.Context) DB() *mongo.Database // contains filtered or unexported methods }
MongoDb implements MongoDb's CRUD operations
type NewMongoDbClient ¶
type NewMongoDbClient struct { // ConnectionUrl which connects to MongoDB atlas or local deployment ConnectionUrl string // DatabaseName with database name DatabaseName string }
NewMongoDbClient takes in the
func (NewMongoDbClient) Add ¶
func (connectionDetails NewMongoDbClient) Add(collectionName string, data interface{}) (*mongo.InsertOneResult, error)
Add can be used to add document to MongoDB
Example ¶
package main import ( "fmt" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Id string `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } testData := data{ Id: "1", Name: "Akshay", } done, err := client.Add("test_collection", testData) if err != nil { panic(err) } fmt.Println("The ID is:", done.InsertedID) }
Output:
func (NewMongoDbClient) AddMany ¶ added in v1.1.0
func (connectionDetails NewMongoDbClient) AddMany(collectionName string, data []interface{}) (*mongo.InsertManyResult, error)
AddMany can be used to add multiple documents to MongoDB
Example ¶
package main import ( "fmt" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Id string `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } var testData = []interface{}{ data{ Id: "1", Name: "Akshay", }, data{ Id: "2", Name: "Raj", }, } done, err := client.AddMany("test_collection", testData) if err != nil { panic(err) } fmt.Println("The ID is:", done.InsertedIDs) }
Output:
func (NewMongoDbClient) Collection ¶
func (connectionDetails NewMongoDbClient) Collection(collectionName string) (*mongo.Collection, *mongo.Client, context.Context)
Collection returns mongo.Collection
Note: Do not forget to do - defer client.Disconnect(ctx)
func (NewMongoDbClient) DB ¶
func (connectionDetails NewMongoDbClient) DB() *mongo.Database
DB returns mongo.Database
func (NewMongoDbClient) Delete ¶
func (connectionDetails NewMongoDbClient) Delete(collectionName string, id string) (*mongo.DeleteResult, error)
Delete deletes a document by ID only.
Example ¶
package main import ( "fmt" "github.com/akshaybabloo/mongo" ) func main() { client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } deleted, err := client.Delete("test_collection", "1") if err != nil { panic(err) } fmt.Println("Deleted items:", deleted.DeletedCount) }
Output:
func (NewMongoDbClient) Get ¶
func (connectionDetails NewMongoDbClient) Get(collectionName string, id string) *mongo.SingleResult
Get finds one document based on "id" and not "_id"
Example ¶
package main import ( "fmt" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Id int `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } var decodeData data output := client.Get("test_collection", "2").Decode(&decodeData) if output != nil { panic("No data found.") } fmt.Println(decodeData) }
Output:
func (NewMongoDbClient) GetAll ¶ added in v1.1.0
func (connectionDetails NewMongoDbClient) GetAll(collectionName string, id string, result interface{}) error
GetAll finds all documents by "id" and not "_id".
The 'result' parameter needs to be a pointer.
Example ¶
package main import ( "fmt" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Id string `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } var testData []data err := client.GetAll("test_collection", "1", &data{}) if err != nil { panic(err) } fmt.Println("The ID is:", testData) }
Output:
func (NewMongoDbClient) GetAllCustom ¶ added in v1.2.0
func (connectionDetails NewMongoDbClient) GetAllCustom(collectionName string, filter interface{}, result interface{}) error
GetAll finds all documents by filter - bson.M{}, bson.A{}, or bson.D{}.
The 'result' parameter needs to be a pointer.
Example ¶
package main import ( "fmt" "go.mongodb.org/mongo-driver/bson" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Id string `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } var testData []data err := client.GetAllCustom("test_collection", bson.M{"id": "1"}, &data{}) if err != nil { panic(err) } fmt.Println("The ID is:", testData) }
Output:
func (NewMongoDbClient) GetCustom ¶ added in v1.2.0
func (connectionDetails NewMongoDbClient) GetCustom(collectionName string, filter interface{}) *mongo.SingleResult
Get finds one document by a filter - bson.M{}, bson.A{}, or bson.D{}
Example ¶
package main import ( "fmt" "go.mongodb.org/mongo-driver/bson" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Id int `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } var decodeData data output := client.GetCustom("test_collection", bson.M{"id": "2"}).Decode(&decodeData) if output != nil { panic("No data found.") } fmt.Println(decodeData) }
Output:
func (NewMongoDbClient) Update ¶
func (connectionDetails NewMongoDbClient) Update(collectionName string, id string, data interface{}) (*mongo.UpdateResult, error)
Update can be used to update values by it's ID
Example ¶
package main import ( "fmt" "github.com/akshaybabloo/mongo" ) func main() { type data struct { Name string `bson:"name"` } client := mongo.NewMongoDbClient{ ConnectionUrl: "mongodb://localhost:27017/?retryWrites=true&w=majority", DatabaseName: "test", } testData := data{ Name: "Akshay", } updated, err := client.Update("test_collection", "1", testData) if err != nil { panic(err) } fmt.Println("Modified items:", updated.ModifiedCount) }
Output: