builder

package module
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

README

Filter Builder For MongoDB Go Driver

Installation

go get github.com/JsyTech/mongo-filter-builder

Usage

Here shows a simple usage for query a struct with specific filed and value. More usages can be found in test files.

import (
	builder "github.com/JsyTech/mongo-filter-builder"
)

// Suppose we have a struct looks like this.
// And we would like to find its data from the mongodb.
type CustomStruct struct {
  Name string `bson:"name"`
  CapName int `bson:"capName"`
}

func main() {
  // To build a simple filter
  filter := builder.New().Str("name").Eq("volinda").Build()
  
  ...
  // use the filter in mongo-go-driver's method
  coll.FindOne(ctx, filter)
  
  
  // condtions are in AND mode as default, you can use Or() to compose more condtions.
  andFilter := builder.New().
  Str("name").Eq("volinda").
  Str("capName").Eq("VOLINDA").Build()
  
  
  // the same as in bson:
  // $or: [
  //   {name: "volinda", capName: "VOLINDA"},
  //   {name: "phile"}
  // ]
  orFilter := builder.New().
  Str("name").Eq("volinda").
  Str("capName").Eq("VOLINDA").
  Or().
  Str("name").Eq("phile").Build()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder represents a filter builder.

func Auto added in v0.0.12

func Auto(val any) *Builder

Auto constructs a new Builder with Builder.Auto method.

func New

func New() *Builder

New constructs a new Builder.

func (*Builder) Any

func (b *Builder) Any(key string) *cond

Any constructs a condition without type restricted.

func (*Builder) AnyMap

func (b *Builder) AnyMap(key string, m bson.M) *Builder

AnyMap will set the given map to current condition.

func (*Builder) Auto added in v0.0.12

func (b *Builder) Auto(queryStruct any) *Builder

Auto will construct suitable eq filter as possible as it can.

queryStruct shuold be a stuct contains query fields (optionally with bson tags).

If bson tag is provided on field, the tag will be used as the key of cond, otherwise snake case of field's name will be used as default.

If it's a pointer:

  • a pointer to a struct: it will try to dereference it.

  • a nil pointer: do nothing.

*Anything else will lead to a panic.

func (*Builder) AutoWithKey added in v0.0.12

func (b *Builder) AutoWithKey(key string, val any) *Builder

AutoWithKey try to add eq cond with provided key and val.

If val is a pointer:

Cond will be built if the val is one of following type and the val is non-zero value:
	- array, slice
	- bool
	- string
	- int, int8 and other ints.
	- uint, uint8 and other uints.
	- float32, float64

If val is not a pointer:

Cond will be built if the pointer is not nil.

func (*Builder) Build

func (b *Builder) Build() bson.M

Build builds final filter and returns it as bson.M.

func (*Builder) Date

func (b *Builder) Date(key string, defaultFormat ...string) *dateCond

func (*Builder) Flush

func (b *Builder) Flush() *Builder

Flush restes the builder to initial state

func (*Builder) Num

func (b *Builder) Num(key string) *numCond

Num indicates the builder to build a condtion for number type.

func (*Builder) Oid

func (b *Builder) Oid(key ...string) *oidCond

Oid key will use _id as default

func (*Builder) Or

func (b *Builder) Or() *Builder

Or appends b.curMap to b.condMaps, and b.curMap will be assigned to a new empty map. Thus if finally b.condMaps's len is bigger than 1, then the final filter will wraps all maps into a $or condtion.

func (*Builder) RemoveCond added in v0.0.18

func (b *Builder) RemoveCond(key string, acrossOrCond ...bool) *Builder

RemoveCond removes given key that has been added to the builder. Elimination will across all conditions if acrossOrCond is given true.

func (*Builder) Str

func (b *Builder) Str(key string) *strCond

WantNum indicates the builder to build a condtion for string type.

Jump to

Keyboard shortcuts

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