Skip to content

Commit 2b58f9a

Browse files
add find collation option (#254)
1 parent 48e2abe commit 2b58f9a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package qmgo
1515

16+
import "go.mongodb.org/mongo-driver/mongo/options"
17+
1618
// CollectionI
1719
// 集合操作接口
1820
//type CollectionI interface {
@@ -48,6 +50,7 @@ type CursorI interface {
4850

4951
// QueryI Query interface
5052
type QueryI interface {
53+
Collation(collation *options.Collation) QueryI
5154
Sort(fields ...string) QueryI
5255
Select(selector interface{}) QueryI
5356
Skip(n int64) QueryI

query.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ type Query struct {
3737
skip *int64
3838
batchSize *int64
3939
noCursorTimeout *bool
40+
collation *options.Collation
4041

4142
ctx context.Context
4243
collection *mongo.Collection
4344
opts []qOpts.FindOptions
4445
registry *bsoncodec.Registry
4546
}
4647

48+
func (q *Query) Collation(collation *options.Collation) QueryI {
49+
newQ := q
50+
newQ.collation = collation
51+
return newQ
52+
}
53+
4754
func (q *Query) NoCursorTimeout(n bool) QueryI {
4855
newQ := q
4956
newQ.noCursorTimeout = &n
@@ -128,6 +135,9 @@ func (q *Query) One(result interface{}) error {
128135
}
129136
opt := options.FindOne()
130137

138+
if q.collation != nil {
139+
opt.SetCollation(q.collation)
140+
}
131141
if q.sort != nil {
132142
opt.SetSort(q.sort)
133143
}
@@ -163,6 +173,9 @@ func (q *Query) All(result interface{}) error {
163173
}
164174
}
165175
opt := options.Find()
176+
if q.collation != nil {
177+
opt.SetCollation(q.collation)
178+
}
166179
if q.sort != nil {
167180
opt.SetSort(q.sort)
168181
}

0 commit comments

Comments
 (0)