mongo-operators

module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: MIT

README

mongo-operators

godev build status

Utility functions to create mongodb operator.

Why

  • IDE hint.
  • Avoid typo.
  • Type check for literal options.
  • Shortcut for common operator combinations.

Usage

go get github.com/NateScarlet/mongo-operators
import (
    "context"
    "github.com/mongodb/mongo-go-driver/mongo"
    "github.com/mongodb/mongo-go-driver/mongo/bson/primitive"
    q "github.com/NateScarlet/mongo-operators/pkg/query"
    a "github.com/NateScarlet/mongo-operators/pkg/aggregation"
)

type M = primitive.M
type A = primitive.A

func find(ctx context.Context, col *mongo.Collection) {
    col.FindOne(ctx, M{"name": q.In(A{"foo", "bar"})})
}

func update(ctx context.Context, col *mongo.Collection) {
    col.UpdateOne(ctx, M{}, q.Update(
        q.Set(M{"name": "foo"}),
        q.Push(M{"tags": "bar"}),
    ))
}

func aggregation(ctx context.Context, col *mongo.Collection) {
    col.Aggregate(ctx, A{
        a.Match(M{"name": "foo"}),
        a.Unwind("tags"),
    })
}

Directories

Path Synopsis
pkg
aggregation
Package aggregation contains helper functions to construct [mongodb aggregation operators](https://docs.mongodb.com/manual/reference/operator/)
Package aggregation contains helper functions to construct [mongodb aggregation operators](https://docs.mongodb.com/manual/reference/operator/)
query
Package query contains helper functions to construct [mongodb query and project operators](https://docs.mongodb.com/manual/reference/operator/)
Package query contains helper functions to construct [mongodb query and project operators](https://docs.mongodb.com/manual/reference/operator/)

Jump to

Keyboard shortcuts

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