You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
795 B
34 lines
795 B
5 years ago
|
package database
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"go.mongodb.org/mongo-driver/mongo"
|
||
|
|
||
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||
|
)
|
||
|
|
||
|
// LoadStream function loads documents one by one into a target channel
|
||
|
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
|
||
|
|
||
|
var cursor *mongo.Cursor
|
||
|
var err error
|
||
|
|
||
|
composed := query.Compose(filters...)
|
||
|
hasPreloader, _ := composed.Preloader()
|
||
|
|
||
|
if hasPreloader {
|
||
|
cursor, err = d.createAggregateLoad(target, composed)
|
||
|
} else {
|
||
|
cursor, err = d.createSimpleLoad(target, composed)
|
||
|
}
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("can't create find result: %w", err)
|
||
|
}
|
||
|
|
||
|
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target}
|
||
|
|
||
|
return l, nil
|
||
|
}
|