https://github.com/natescarlet/mongo-operators
Utility functions to create mongodb operator.
https://github.com/natescarlet/mongo-operators
bson mongodb
Last synced: 7 months ago
JSON representation
Utility functions to create mongodb operator.
- Host: GitHub
- URL: https://github.com/natescarlet/mongo-operators
- Owner: NateScarlet
- License: mit
- Created: 2020-05-14T14:43:19.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-20T06:10:31.000Z (over 3 years ago)
- Last Synced: 2025-01-16T16:26:55.078Z (9 months ago)
- Topics: bson, mongodb
- Language: Go
- Size: 96.7 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mongo-operators
[](https://pkg.go.dev/github.com/NateScarlet/mongo-operators)
[](https://github.com/NateScarlet/mongo-operators/actions)Utility functions to create [mongodb](https://www.mongodb.com/) operator.
## Why
- IDE hint.
- Avoid typo.
- Type check for literal options.
- Shortcut for common operator combinations.## Usage
```shell
go get github.com/NateScarlet/mongo-operators
``````Go
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.Afunc 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.MergeOperators(
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"),
})
}```