mongque

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

README

go-mongque

(Pronounced mong-key) Simple utility to generate MongoDB Filter for queries using generics.

Usage

Import

import "github.com/doechyeah/go-mongque"

Install

go get github.com/doechyeah/go-mongque
Example
type Document struct {
    Id      primitive.ObjectID `bson:"_id"`
    Name    string             `bson:"name"`
    Summary string             `bson:"summary"`
    Score   int                `bson:"score"`
}

client, _ := mongo.Connect(context.TODO(), clientOptions)

coll := client.Database("test").Collection("sample")

filter := mongque.NewFilter( mongoque.Eq("name", "John"), mongque.Lte("score", 60) )
/**
bson.M {
    "summary" : bson.M{"$eq": "This is a test summary"},
    "score": bson.M{"$lte": 60},
}
*/

var Doc Document
_ = coll.FindOne(context.TODO(), filter).Decode(&doc)

Features

Initial version: 0.1.0

Operators are based on the MongoDB query operations https://www.mongodb.com/docs/manual/reference/operator/query/

Currently supports the following query types:

  • Comparator
  • Logical

Additional operators are planned to be developed alongside support for building aggregation pipelines.

To Be Added

Priority is listed in order.

Operators:

  • [] Geospatial
  • [] Projection
  • [] Array
  • [] Element
  • [] Evaluation
  • [] Bitwise

Miscellaneous operators such as $comment and $rand will not be added.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFilter

func NewFilter[T Operable](fields ...Field[T]) bson.M

NewFilter generates a new filter with fields. Returns a bson.M object

func NewFilterD

func NewFilterD[T Operable](fields ...Field[T]) bson.D

NewFilterD generates a new filter with fields. Returns a bson.D object

Types

type Field

type Field[T Operable] struct {
	// contains filtered or unexported fields
}

Field is the base object type to generate the filters.

func And

func And[T Operable](name string, values ...Field[T]) Field[logical]

And create a new Field for a filter with the $and logical operator

func AndBson

func AndBson(name string, values bson.D) Field[logical]

AndBson create a new Field for a filter with the $and logical operator with a bson.D object

func Eq

func Eq(name string, val any) Field[comparator]

Eq create a new Field for a filter with the $eq comparator

func Gt

func Gt(name string, val any) Field[comparator]

Gt create a new Field for a filter with the $lt comparator

func Gte

func Gte(name string, val any) Field[comparator]

Gte create a new Field for a filter with the $gte comparator

func In

func In(name string, val any) Field[comparator]

In create a new Field for a filter with the $in comparator

func Lt

func Lt(name string, val any) Field[comparator]

Lt create a new Field for a filter with the $lt comparator

func Lte

func Lte(name string, val any) Field[comparator]

Lte create a new Field for a filter with the $lte comparator

func Neq

func Neq(name string, val any) Field[comparator]

Neq create a new Field for a filter with the $neq comparator

func Nin

func Nin(name string, val any) Field[comparator]

Nin create a new Field for a filter with the $nin comparator

func Nor

func Nor[T Operable](name string, values ...Field[T]) Field[logical]

NorOperable create a new Field for a filter with the $and logical operator

func NorBson

func NorBson(name string, values bson.D) Field[logical]

NorBson create a new Field for a filter with the $nor logical operator with a bson.D object

func Not

func Not[T Operable](name string, values ...Field[T]) Field[logical]

Not create a new Field for a filter with the $and logical operator

func NotBson

func NotBson(name string, val bson.D) Field[logical]

NotBson create a new Field for a filter with the $not logical operator with a bson.D object

func Or

func Or[T Operable](name string, values ...Field[T]) Field[logical]

Or create a new Field for a filter with the $and logical operator

func OrBson

func OrBson(name string, values ...bson.D) Field[logical]

OrBson create a new Field for a filter with the $or logical operator with a bson.D object

func (Field[Operable]) Filter

func (f Field[Operable]) Filter() bson.M

Filter converts the field to a bson.M filter

func (Field[Operable]) FilterD

func (f Field[Operable]) FilterD() bson.D

FilterD converts the field to bson.D filter

func (Field[Operable]) FilterE

func (f Field[Operable]) FilterE() bson.E

FilterE converts the field to a bson.E object. #Warning this is not a valid filter and not to be used by itself. Use within a bson.D object.

func (Field[Operable]) SetName

func (f Field[Operable]) SetName(name string) Field[Operable]

SetName sets a the field name for a filter.

func (Field[Operable]) SetValue

func (f Field[Operable]) SetValue(value string) Field[Operable]

SetValue sets the field value for a filter

type Operable

type Operable interface {
	comparator | logical
	// contains filtered or unexported methods
}

Operable defines the acceptable type of operations to set a field

Jump to

Keyboard shortcuts

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