Add filter conversion
This commit is contained in:
97
filter.go
Normal file
97
filter.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package mongo
|
||||
|
||||
// import "runtime"
|
||||
import "log"
|
||||
|
||||
type Query struct {
|
||||
Filter map[string]any
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
Name string
|
||||
Op string
|
||||
Value string
|
||||
}
|
||||
|
||||
func makeFilter(name string, value any) *Filter {
|
||||
|
||||
log.Printf("type is %T", value)
|
||||
|
||||
var op string
|
||||
|
||||
var v string
|
||||
|
||||
switch value.(type) {
|
||||
case string:
|
||||
op = "eq"
|
||||
v = value.(string)
|
||||
case map[string]any:
|
||||
vMap := value.(map[string]any)
|
||||
for kk, vv := range vMap {
|
||||
op = kk
|
||||
v = vv.(string)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
op = "eq"
|
||||
}
|
||||
|
||||
o := &Filter{
|
||||
Name: name,
|
||||
Op: op,
|
||||
Value: v,
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func Mongofy(q *Query) map[string]any {
|
||||
|
||||
conditions := make([]map[string]interface{}, 0)
|
||||
|
||||
logic := "and"
|
||||
|
||||
// runtime.Breakpoint()
|
||||
for k, v := range q.Filter {
|
||||
if k == "_logic" {
|
||||
logic = v.(string)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
f := makeFilter(k, v)
|
||||
|
||||
log.Printf("=> filter: %#v", f)
|
||||
|
||||
mongoOp := mapToOp(f.Op)
|
||||
|
||||
conditions = append(conditions, map[string]any{ k: map[string]any{ mongoOp: f.Value }})
|
||||
}
|
||||
|
||||
if logic == "or" {
|
||||
return map[string]any{
|
||||
"$or": conditions,
|
||||
}
|
||||
}
|
||||
// if filter == nil {
|
||||
// return filter
|
||||
// }
|
||||
|
||||
return map[string]any{
|
||||
"$and": conditions,
|
||||
}
|
||||
}
|
||||
|
||||
func mapToOp(v string) string {
|
||||
switch v {
|
||||
case "eq":
|
||||
return "$eq"
|
||||
case "includes":
|
||||
return "$regex"
|
||||
default:
|
||||
log.Printf("WARN: no conversion")
|
||||
return v
|
||||
}
|
||||
}
|
||||
52
find.go
52
find.go
@@ -1,15 +1,9 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
// "log"
|
||||
"context"
|
||||
// "fmt"
|
||||
// "encoding/base64"
|
||||
|
||||
// "go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
|
||||
// "git.gsuntres.com/boxtep/boxtep/core"
|
||||
)
|
||||
|
||||
// Find is used to fetch the first page of data.
|
||||
@@ -20,16 +14,7 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs
|
||||
|
||||
pageSize := max(limit, c.Limit)
|
||||
|
||||
|
||||
// id := DecodeCursor(nextCursor)
|
||||
|
||||
// filter["_id"] = bson.M{"_id": bson.M{"$gt": id}}
|
||||
// opts := options.Find().
|
||||
// SetLimit(pageSize + 1).
|
||||
// SetSort(bson.M{"_id": 1})
|
||||
// id := DecodeCursor(nextCursor)
|
||||
sort := bson.M{"_id": 1}
|
||||
// filter["_id"] = bson.M{"_id": bson.M{"$gt": id}}
|
||||
|
||||
pipeline := BuildPaginationPipeline(0, pageSize + 1, filter, sort)
|
||||
|
||||
@@ -91,16 +76,6 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs
|
||||
out["next_cursor"] = nextCursor
|
||||
}
|
||||
|
||||
// _data, err := bson.Marshal(out)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// var r bson.M
|
||||
// if err := bson.Unmarshal(_data, &r); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -110,7 +85,11 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
||||
|
||||
finalLimit := max(limit, c.Limit)
|
||||
|
||||
pipeline := BuildPaginationPipeline(offset, finalLimit, filter, nil)
|
||||
f := Mongofy(&Query{
|
||||
Filter: filter,
|
||||
})
|
||||
|
||||
pipeline := BuildPaginationPipeline(offset, finalLimit, f, nil)
|
||||
|
||||
// 2. Query
|
||||
cursor, err := collection.Aggregate(ctx, pipeline)
|
||||
@@ -163,24 +142,3 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
||||
|
||||
return out, nil
|
||||
}
|
||||
// func (c *MongoClient) FindNext(ctx context.Context, database, name string, filter bson.M, nextCursor string, limit int64) ([]bson.M, error) {
|
||||
// collection := c.GetCollection(database, name)
|
||||
|
||||
// opts := options.Find().
|
||||
// SetLimit(max(limit, c.Limit)).
|
||||
// SetSort()
|
||||
|
||||
// cursor, err := collection.Find(ctx, filter)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// var results []bson.M
|
||||
// if err = cursor.All(ctx, &results); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return results, err
|
||||
// }
|
||||
|
||||
// func EncodeCursor()
|
||||
|
||||
@@ -45,7 +45,7 @@ func (c *MongoClient) CreateIndexes(collection *mongo.Collection, cdef *Collecti
|
||||
|
||||
var keysBson bson.D
|
||||
if err := bson.Unmarshal(keysVal.Value, &keysBson); err != nil {
|
||||
log.Printf("failed to unmarshal keys value %v", err)
|
||||
log.Printf("failed to unmarshal keys value %v, %v", keysVal, err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user