oclib then
This commit is contained in:
@@ -102,12 +102,12 @@ func GetBson(filters *Filters) bson.D {
|
||||
}
|
||||
}
|
||||
if len(orList) > 0 && len(andList) == 0 {
|
||||
f = bson.D{{"$or", orList}}
|
||||
f = bson.D{{Key: "$or", Value: orList}}
|
||||
} else {
|
||||
if len(orList) > 0 {
|
||||
andList = append(andList, bson.M{"$or": orList})
|
||||
}
|
||||
f = bson.D{{"$and", andList}}
|
||||
f = bson.D{{Key: "$and", Value: andList}}
|
||||
}
|
||||
}
|
||||
return f
|
||||
|
||||
@@ -282,12 +282,11 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
|
||||
return res, 200, nil
|
||||
}
|
||||
|
||||
func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.Cursor, int, error) {
|
||||
func (m *MongoDB) Search(filters *dbs.Filters, collection_name string, offset int64, limit int64) (*mongo.Cursor, int, error) {
|
||||
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
|
||||
return nil, 503, err
|
||||
}
|
||||
opts := options.Find()
|
||||
opts.SetLimit(1000)
|
||||
targetDBCollection := CollectionMap[collection_name]
|
||||
if targetDBCollection == nil {
|
||||
return nil, 503, errors.New("collection " + collection_name + " not initialized")
|
||||
@@ -295,6 +294,9 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
|
||||
|
||||
f := dbs.GetBson(filters)
|
||||
|
||||
opts.SetSkip(offset) // OFFSET
|
||||
opts.SetLimit(limit) // LIMIT
|
||||
|
||||
MngoCtx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
// defer cancel()
|
||||
if cursor, err := targetDBCollection.Find(
|
||||
@@ -329,7 +331,8 @@ func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name stri
|
||||
return res, 200, nil
|
||||
}
|
||||
|
||||
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
|
||||
func (m *MongoDB) LoadAll(collection_name string, offset int64, limit int64) (*mongo.Cursor, int, error) {
|
||||
|
||||
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
|
||||
return nil, 503, err
|
||||
}
|
||||
@@ -337,8 +340,10 @@ func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
|
||||
|
||||
MngoCtx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
//defer cancel()
|
||||
|
||||
res, err := targetDBCollection.Find(MngoCtx, bson.D{})
|
||||
findOptions := options.Find()
|
||||
findOptions.SetSkip(offset) // OFFSET
|
||||
findOptions.SetLimit(limit) // LIMIT
|
||||
res, err := targetDBCollection.Find(MngoCtx, bson.D{}, findOptions)
|
||||
if err != nil {
|
||||
// m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
|
||||
return nil, 404, err
|
||||
|
||||
Reference in New Issue
Block a user