-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
51 lines (46 loc) · 1.39 KB
/
builder.go
File metadata and controls
51 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mongoq
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// BuildFilter converts the query's root filter node into a MongoDB filter document (bson.M).
// It returns an empty document (bson.M{}) if the query has no filter, which the
// MongoDB Go driver treats as a match-all query. Passing nil to Collection.Find
// would return ErrNilDocument, so we never return nil here.
func BuildFilter(q *Query) (bson.M, error) {
if q.filter == nil {
return bson.M{}, nil
}
return q.filter.ToBSON()
}
// BuildOptions constructs a FindOptions object from the query's limit, offset, sort, and projection.
func BuildOptions(q *Query) *options.FindOptions {
opts := options.Find()
if q.limit > 0 {
opts.SetLimit(q.limit)
}
if q.offset > 0 {
opts.SetSkip(q.offset)
}
if len(q.sort) > 0 {
sortDoc := bson.D{}
for _, s := range q.sort {
sortDoc = append(sortDoc, bson.E{Key: s.Field, Value: s.Order.ToInt()})
}
opts.SetSort(sortDoc)
}
if q.projection != nil {
opts.SetProjection(q.projection)
}
return opts
}
// BuildMongoQuery is a convenience function that returns both the filter and options.
// It is equivalent to calling BuildFilter and BuildOptions.
func BuildMongoQuery(q *Query) (bson.M, *options.FindOptions, error) {
filter, err := BuildFilter(q)
if err != nil {
return nil, nil, err
}
opts := BuildOptions(q)
return filter, opts, nil
}