xmongo

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2023 License: Unlicense Imports: 6 Imported by: 0

README

MongoDB's Go generic util Go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any](ctx context.Context, cursor *mongo.Cursor) ([]*T, error)

Decode with generic support.

func Find

func Find[T any](ctx context.Context, coll *mongo.Collection, filter interface{}, opts ...*options.FindOptions) ([]*T, error)
Example
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
	panic(err)
}
collection := client.Database(databaseName).Collection(collectionName)
res, err := xmongo.Find[Record](ctx, collection, bson.M{})
if err != nil {
	panic(err)
}
fmt.Println(res)
Output:

func FindOne

func FindOne[T any](ctx context.Context, coll *mongo.Collection, filter interface{}, opts ...*options.FindOneOptions) (*T, error)
Example
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
	panic(err)
}
collection := client.Database(databaseName).Collection(collectionName)
res, err := xmongo.FindOne[Record](ctx, collection, bson.M{})
if err != nil {
	panic(err)
}
fmt.Println(res)
Output:

Types

type Repo

type Repo[M any] struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/caiyunapp/xmongo"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type Record struct {
	OID primitive.ObjectID `bson:"_id"`
	Msg string             `bson:"msg"`
}

var repo *xmongo.Repo[Record]

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	fmt.Println(repo.FindOne(ctx, bson.M{}))
	fmt.Println(repo.Find(ctx, bson.M{}))
	insertRes, err := repo.InsertOne(ctx, Record{OID: primitive.NewObjectID(), Msg: "insert_one"})
	fmt.Println(insertRes, err)
}
Output:

func NewRepo

func NewRepo[M any](collection *mongo.Collection) (*Repo[M], error)

func (Repo[M]) Aggregate

func (r Repo[M]) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)

func (Repo[M]) Find

func (r Repo[M]) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]*M, error)

func (Repo[M]) FindOne

func (r Repo[M]) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*M, error)

func (Repo[M]) Get

func (r Repo[M]) Get(ctx context.Context, oid primitive.ObjectID) (*M, error)

func (Repo[M]) InsertMany

func (r Repo[M]) InsertMany(ctx context.Context, docs []M, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func (Repo[M]) InsertOne

func (r Repo[M]) InsertOne(ctx context.Context, doc M, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL