Skip to content

Instantly share code, notes, and snippets.

@focusj
Created January 21, 2020 07:37
Show Gist options
  • Select an option

  • Save focusj/f0de11a231b9e5529ebbf67427e3b5b5 to your computer and use it in GitHub Desktop.

Select an option

Save focusj/f0de11a231b9e5529ebbf67427e3b5b5 to your computer and use it in GitHub Desktop.
mongo query language golang
package driver
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type Filter struct {
ds map[string]interface{}
}
//$eq Matches values that are equal to a specified value.
//$gt Matches values that are greater than a specified value.
//$gte Matches values that are greater than or equal to a specified value.
//$in Matches any of the values specified in an array.
//$lt Matches values that are less than a specified value.
//$lte Matches values that are less than or equal to a specified value.
//$ne Matches all values that are not equal to a specified value.
//$nin Matches none of the values specified in an array.
func Eq(field string, value interface{}) bson.D {
return cmd("$eq", field, value)
}
func Gt(field string, value interface{}) bson.D {
return cmd("$gt", field, value)
}
func Gte(field string, value interface{}) bson.D {
return cmd("$gte", field, value)
}
func In(field string, values ...interface{}) bson.D {
vals := bson.A{}
for value := range values {
vals = append(vals, value)
}
return cmd("$in", field, vals)
}
func Lt(field string, value interface{}) bson.D {
return cmd("$lt", field, value)
}
func Lte(field string, value interface{}) bson.D {
return cmd("$lte", field, value)
}
func Ne(field string, value interface{}) bson.D {
return cmd("$ne", field, value)
}
func Nin(field string, values ...interface{}) bson.D {
vals := bson.A{}
for value := range values {
vals = append(vals, value)
}
return cmd("$nin", field, vals)
}
func cmd(operator string, field string, value interface{}) bson.D {
return bson.D{{field, bson.D{{operator, value}}}}
}
//pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"$or",
// bson.A{
// bson.D{{"fullDocument.username", "alice"}},
// bson.D{{"operationType", "delete"}}}}},
//}}}
func test() {
var coll mongo.Collection
var filter interface{}
var ctx context.Context
coll.Find(ctx, filter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment