Skip to content

Commit ea859c0

Browse files
johnlawsharrisondomodwyer
authored andcommitted
Implement collation option for Collection.Pipe (globalsign#144)
1 parent 243904d commit ea859c0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

session.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,7 @@ type Pipe struct {
25102510
allowDisk bool
25112511
batchSize int
25122512
maxTimeMS int64
2513+
collation *Collation
25132514
}
25142515

25152516
type pipeCmd struct {
@@ -2519,6 +2520,7 @@ type pipeCmd struct {
25192520
Explain bool `bson:",omitempty"`
25202521
AllowDisk bool `bson:"allowDiskUse,omitempty"`
25212522
MaxTimeMS int64 `bson:"maxTimeMS,omitempty"`
2523+
Collation *Collation `bson:"collation,omitempty"`
25222524
}
25232525

25242526
type pipeCmdCursor struct {
@@ -2539,6 +2541,7 @@ type pipeCmdCursor struct {
25392541
// http://docs.mongodb.org/manual/applications/aggregation
25402542
// http://docs.mongodb.org/manual/tutorial/aggregation-examples
25412543
//
2544+
25422545
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
25432546
session := c.Database.Session
25442547
session.m.RLock()
@@ -2572,6 +2575,7 @@ func (p *Pipe) Iter() *Iter {
25722575
Pipeline: p.pipeline,
25732576
AllowDisk: p.allowDisk,
25742577
Cursor: &pipeCmdCursor{p.batchSize},
2578+
Collation: p.collation,
25752579
}
25762580
if p.maxTimeMS > 0 {
25772581
cmd.MaxTimeMS = p.maxTimeMS
@@ -2761,6 +2765,22 @@ func (p *Pipe) SetMaxTime(d time.Duration) *Pipe {
27612765
return p
27622766
}
27632767

2768+
// Collation allows to specify language-specific rules for string comparison,
2769+
// such as rules for lettercase and accent marks.
2770+
// When specifying collation, the locale field is mandatory; all other collation
2771+
// fields are optional
2772+
//
2773+
// Relevant documentation:
2774+
//
2775+
// https://docs.mongodb.com/manual/reference/collation/
2776+
//
2777+
func (p *Pipe) Collation(collation *Collation) *Pipe {
2778+
if collation != nil {
2779+
p.collation = collation
2780+
}
2781+
return p
2782+
}
2783+
27642784
// LastError the error status of the preceding write operation on the current connection.
27652785
//
27662786
// Relevant documentation:

session_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,6 +4617,36 @@ func (s *S) TestPipeExplain(c *C) {
46174617
c.Assert(result.Ok, Equals, 1)
46184618
}
46194619

4620+
func (s *S) TestPipeCollation(c *C) {
4621+
if !s.versionAtLeast(2, 1) {
4622+
c.Skip("Pipe only works on 2.1+")
4623+
}
4624+
if !s.versionAtLeast(3, 3, 12) {
4625+
c.Skip("collations being released with 3.4")
4626+
}
4627+
4628+
session, err := mgo.Dial("localhost:40001")
4629+
c.Assert(err, IsNil)
4630+
defer session.Close()
4631+
4632+
coll := session.DB("mydb").C("mycoll")
4633+
beatles := []string{"John", "RINGO", "George", "Paul"}
4634+
for _, n := range beatles {
4635+
err := coll.Insert(M{"name": n})
4636+
c.Assert(err, IsNil)
4637+
}
4638+
4639+
collation := &mgo.Collation{
4640+
Locale: "en",
4641+
Strength: 1, // ignore case/diacritics
4642+
}
4643+
var result []struct{ Name string }
4644+
err = coll.Pipe([]M{{"$match": M{"name": "ringo"}}}).Collation(collation).All(&result)
4645+
c.Assert(err, IsNil)
4646+
c.Assert(len(result), Equals, 1)
4647+
c.Assert(result[0].Name, Equals, "RINGO")
4648+
}
4649+
46204650
func (s *S) TestBatch1Bug(c *C) {
46214651
session, err := mgo.Dial("localhost:40001")
46224652
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)