This commit is contained in:
mr 2024-08-02 13:10:27 +02:00
parent d0e2778246
commit e9dd17ae87
3 changed files with 36 additions and 17 deletions

View File

@ -35,26 +35,49 @@ func (m Operator) String() string {
return str[m]
}
func (m Operator) ToMongoOperator() string {
func (m Operator) ToMongoEOperator(k string, value interface{}) bson.E {
switch m {
case LIKE:
return "$regex"
return bson.E{Key: k, Value: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
case EXISTS:
return "$exists"
return bson.E{Key: k, Value: bson.M{"$exists": ToValueOperator(StringToOperator(m.String()), value)}}
case IN:
return "$in"
return bson.E{Key: k, Value: bson.M{"$in": ToValueOperator(StringToOperator(m.String()), value)}}
case GTE:
return "$gte"
return bson.E{Key: k, Value: bson.M{"$gte": ToValueOperator(StringToOperator(m.String()), value)}}
case GT:
return "$gt"
return bson.E{Key: k, Value: bson.M{"$gt": ToValueOperator(StringToOperator(m.String()), value)}}
case LTE:
return "$lte"
return bson.E{Key: k, Value: bson.M{"$lte": ToValueOperator(StringToOperator(m.String()), value)}}
case LT:
return "$lt"
return bson.E{Key: k, Value: bson.M{"$lt": ToValueOperator(StringToOperator(m.String()), value)}}
case EQUAL:
return "$match"
return bson.E{Key: k, Value: value}
default:
return "$regex"
return bson.E{Key: k, Value: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
}
}
func (m Operator) ToMongoOperator(k string, value interface{}) bson.M {
switch m {
case LIKE:
return bson.M{k: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
case EXISTS:
return bson.M{k: bson.M{"$exists": ToValueOperator(StringToOperator(m.String()), value)}}
case IN:
return bson.M{k: bson.M{"$in": ToValueOperator(StringToOperator(m.String()), value)}}
case GTE:
return bson.M{k: bson.M{"$gte": ToValueOperator(StringToOperator(m.String()), value)}}
case GT:
return bson.M{k: bson.M{"$gt": ToValueOperator(StringToOperator(m.String()), value)}}
case LTE:
return bson.M{k: bson.M{"$lte": ToValueOperator(StringToOperator(m.String()), value)}}
case LT:
return bson.M{k: bson.M{"$lt": ToValueOperator(StringToOperator(m.String()), value)}}
case EQUAL:
return bson.M{k: value}
default:
return bson.M{k: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
}
}

View File

@ -267,17 +267,13 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
f := bson.D{}
if filters != nil {
for k, filter := range filters.Or {
orList = append(orList, bson.M{
k: bson.M{dbs.StringToOperator(filter.Operator).ToMongoOperator(): dbs.ToValueOperator(dbs.StringToOperator(filter.Operator), filter.Value)}})
orList = append(orList, dbs.StringToOperator(filter.Operator).ToMongoOperator(k, filter.Value))
}
if len(orList) > 0 {
f = bson.D{{"$or", orList}}
}
for k, filter := range filters.And {
f = append(f, bson.E{
k, bson.M{
dbs.StringToOperator(filter.Operator).ToMongoOperator(): dbs.ToValueOperator(dbs.StringToOperator(filter.Operator), filter.Value),
}})
f = append(f, dbs.StringToOperator(filter.Operator).ToMongoEOperator(k, filter.Value))
}
}

View File

@ -107,8 +107,8 @@ func (wfa *workflowMongoAccessor) execute(workflow *Workflow) {
},
}
resource, _, err := accessor.Search(filters, "")
fmt.Println(resource)
if err == nil && len(resource) > 0 {
fmt.Println(workflow.Processings, resource[0].GetID())
accessor.UpdateOne(&workspace.Workspace{
Active: true,
ResourceSet: resources.ResourceSet{