oc-lib/dbs/mongo/mongo_utils.go

56 lines
1.5 KiB
Go
Raw Normal View History

2024-07-17 17:19:42 +02:00
package mongo
import (
"go.mongodb.org/mongo-driver/bson"
2024-07-18 11:51:12 +02:00
"go.mongodb.org/mongo-driver/bson/primitive"
2024-07-17 17:19:42 +02:00
"go.mongodb.org/mongo-driver/mongo"
)
// Will store the created collection object for a faster access
var CollectionMap map[string]*mongo.Collection
var IndexesMap map[string][]mongo.IndexModel
func init() {
CollectionMap = make(map[string]*mongo.Collection)
IndexesMap = make(map[string][]mongo.IndexModel)
IndexesMap["data"] = append(IndexesMap["data"], mongo.IndexModel{Keys: bson.D{
2024-07-18 11:51:12 +02:00
{Key: "description", Value: "text"},
{Key: "example", Value: "text"}},
2024-07-17 17:19:42 +02:00
})
IndexesMap["datacenter"] = append(IndexesMap["datacenter"], mongo.IndexModel{Keys: bson.D{
2024-07-18 11:51:12 +02:00
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"}},
2024-07-17 17:19:42 +02:00
})
IndexesMap["storage"] = append(IndexesMap["storage"], mongo.IndexModel{Keys: bson.D{
2024-07-18 11:51:12 +02:00
{Key: "description", Value: "text"},
{Key: "example", Value: "text"}},
2024-07-17 17:19:42 +02:00
})
IndexesMap["processing"] = append(IndexesMap["processing"], mongo.IndexModel{Keys: bson.D{
2024-07-18 11:51:12 +02:00
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"},
2024-07-17 17:19:42 +02:00
},
})
IndexesMap["workflow"] = append(IndexesMap["workflow"], mongo.IndexModel{Keys: bson.D{
2024-07-18 11:51:12 +02:00
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"},
2024-07-17 17:19:42 +02:00
},
})
2024-07-18 11:51:12 +02:00
}
func GetObjIDFromString(id string) interface{} {
objectID, err := primitive.ObjectIDFromHex(id)
if err == nil {
return objectID
}
return id
}