mongo and or

This commit is contained in:
mr 2024-08-02 15:36:52 +02:00
parent efa73d8a45
commit 24a277333d

View File

@ -268,6 +268,7 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
opts.SetLimit(100)
targetDBCollection := CollectionMap[collection_name]
orList := bson.A{}
andList := bson.A{}
f := bson.D{}
if filters != nil {
for k, filter := range filters.Or {
@ -275,14 +276,18 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
orList = append(orList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
if len(orList) > 0 {
f = bson.D{{"$or", orList}}
}
for k, filter := range filters.And {
for _, ff := range filter {
f = append(f, dbs.StringToOperator(ff.Operator).ToMongoEOperator(k, ff.Value))
andList = append(andList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
if len(orList) > 0 && len(andList) == 0 {
f = bson.D{{"$or", orList}}
} else if len(orList) == 0 && len(andList) > 0 {
f = bson.D{{"$and", andList}}
} else if len(orList) > 0 && len(andList) > 0 {
f = bson.D{{"$and", andList}, {"$or", orList}}
}
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)