|
|
@ -6,30 +6,30 @@ import ( |
|
|
|
"github.com/mainnika/mongox-go-driver/mongox/query" |
|
|
|
"github.com/mainnika/mongox-go-driver/mongox/query" |
|
|
|
"go.mongodb.org/mongo-driver/bson" |
|
|
|
"go.mongodb.org/mongo-driver/bson" |
|
|
|
"go.mongodb.org/mongo-driver/mongo" |
|
|
|
"go.mongodb.org/mongo-driver/mongo" |
|
|
|
"go.mongodb.org/mongo-driver/mongo/options" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// LoadBinary function loads a first single target document by a query
|
|
|
|
// LoadBinary function loads a first single target document by a query
|
|
|
|
func LoadBinary(db *mongox.Database, target interface{}, filters ...interface{}) (bson.Raw, error) { |
|
|
|
func LoadBinary(db *mongox.Database, target interface{}, filters ...interface{}) (bson.Raw, error) { |
|
|
|
|
|
|
|
|
|
|
|
collection := db.GetCollectionOf(target) |
|
|
|
composed := query.Compose(append(filters, query.Limit(1))...) |
|
|
|
opts := options.FindOne() |
|
|
|
hasPreloader, _ := composed.Preloader() |
|
|
|
composed := query.Compose(filters...) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opts.Sort = composed.Sorter() |
|
|
|
var result *mongo.Cursor |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
result := collection.FindOne(db.Context(), composed.M(), opts) |
|
|
|
if hasPreloader { |
|
|
|
if result.Err() != nil { |
|
|
|
result, err = createAggregateLoad(db, target, composed) |
|
|
|
return nil, errors.InternalErrorf("can't create find one result: %s", result.Err()) |
|
|
|
} else { |
|
|
|
} |
|
|
|
result, err = createSimpleLoad(db, target, composed) |
|
|
|
|
|
|
|
|
|
|
|
bytes, err := result.DecodeBytes() |
|
|
|
|
|
|
|
if err == mongo.ErrNoDocuments { |
|
|
|
|
|
|
|
return nil, errors.NotFoundErrorf("%s", err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, errors.InternalErrorf("can't decode desult: %s", err) |
|
|
|
return nil, errors.InternalErrorf("can't create find result: %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasNext := result.Next(db.Context()) |
|
|
|
|
|
|
|
if !hasNext { |
|
|
|
|
|
|
|
return nil, errors.NotFoundErrorf("can't find result: %s", result.Err()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return bytes, nil |
|
|
|
return result.Current, nil |
|
|
|
} |
|
|
|
} |
|
|
|