adding search func

This commit is contained in:
mr
2024-07-26 13:45:10 +02:00
parent 6c83e54d37
commit 8c97fca96c
12 changed files with 170 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package mongo
import (
"context"
"errors"
"strings"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
@@ -234,6 +235,29 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
return res, 200, nil
}
func (m *MongoDB) Search(search string, filter []string, collection_name string) (*mongo.Cursor, int, error) {
opts := options.Find()
opts.SetLimit(100)
if strings.TrimSpace(search) == "*" {
search = ""
} else {
search = `(?i).*` + search + `.*`
}
targetDBCollection := CollectionMap[collection_name]
list := []bson.M{}
for _, k := range filter {
list = append(list, bson.M{k: bson.M{"$regex": search}})
}
if cursor, err := targetDBCollection.Find(
MngoCtx, bson.M{"$or": list},
opts,
); err != nil {
return nil, 404, err
} else {
return cursor, 200, nil
}
}
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
f := bson.D{}
for k, v := range filter {