diff --git a/dbs/dbs.go b/dbs/dbs.go index 3403615..45e41f6 100644 --- a/dbs/dbs.go +++ b/dbs/dbs.go @@ -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)}} } } diff --git a/dbs/mongo/mongo.go b/dbs/mongo/mongo.go index 109e882..28594ae 100644 --- a/dbs/mongo/mongo.go +++ b/dbs/mongo/mongo.go @@ -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)) } } diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index 1fb3e0b..1603104 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -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{