Skip to content

Commit b37e3c1

Browse files
feliixxdomodwyer
authored andcommitted
Add collation option to collection.Create() (#37)
- Allow specifying the default collation for the collection when creating it. - Add some documentation to query.Collation() method. fix #29
1 parent 934a190 commit b37e3c1

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

session.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,10 @@ type CollectionInfo struct {
28572857
// storage engine in use. The map keys must hold the storage engine
28582858
// name for which options are being specified.
28592859
StorageEngine interface{}
2860+
// Specifies the default collation for the collection.
2861+
// Collation allows users to specify language-specific rules for string
2862+
// comparison, such as rules for lettercase and accent marks.
2863+
Collation *Collation
28602864
}
28612865

28622866
// Create explicitly creates the c collection with details of info.
@@ -2900,6 +2904,10 @@ func (c *Collection) Create(info *CollectionInfo) error {
29002904
if info.StorageEngine != nil {
29012905
cmd = append(cmd, bson.DocElem{"storageEngine", info.StorageEngine})
29022906
}
2907+
if info.Collation != nil {
2908+
cmd = append(cmd, bson.DocElem{"collation", info.Collation})
2909+
}
2910+
29032911
return c.Database.Run(cmd, nil)
29042912
}
29052913

@@ -3039,6 +3047,30 @@ func (q *Query) Sort(fields ...string) *Query {
30393047
return q
30403048
}
30413049

3050+
// Collation allows to specify language-specific rules for string comparison,
3051+
// such as rules for lettercase and accent marks.
3052+
// When specifying collation, the locale field is mandatory; all other collation
3053+
// fields are optional
3054+
//
3055+
// For example, to perform a case and diacritic insensitive query:
3056+
//
3057+
// var res []bson.M
3058+
// collation := &mgo.Collation{Locale: "en", Strength: 1}
3059+
// err = db.C("mycoll").Find(bson.M{"a": "a"}).Collation(collation).All(&res)
3060+
// if err != nil {
3061+
// return err
3062+
// }
3063+
//
3064+
// This query will match following documents:
3065+
//
3066+
// {"a": "a"}
3067+
// {"a": "A"}
3068+
// {"a": "â"}
3069+
//
3070+
// Relevant documentation:
3071+
//
3072+
// https://docs.mongodb.com/manual/reference/collation/
3073+
//
30423074
func (q *Query) Collation(collation *Collation) *Query {
30433075
q.m.Lock()
30443076
q.op.options.Collation = collation

session_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,39 @@ func (s *S) TestCreateCollectionStorageEngine(c *C) {
10201020
c.Assert(err, ErrorMatches, "test is not a registered storage engine for this server")
10211021
}
10221022

1023+
func (s *S) TestCreateCollectionWithCollation(c *C) {
1024+
if !s.versionAtLeast(3, 4) {
1025+
c.Skip("depends on mongodb 3.4+")
1026+
}
1027+
session, err := mgo.Dial("localhost:40001")
1028+
c.Assert(err, IsNil)
1029+
defer session.Close()
1030+
1031+
db := session.DB("mydb")
1032+
coll := db.C("mycoll")
1033+
1034+
info := &mgo.CollectionInfo{
1035+
Collation: &mgo.Collation{Locale: "en", Strength: 1},
1036+
}
1037+
err = coll.Create(info)
1038+
c.Assert(err, IsNil)
1039+
1040+
err = coll.Insert(M{"a": "case"})
1041+
c.Assert(err, IsNil)
1042+
1043+
err = coll.Insert(M{"a": "CaSe"})
1044+
c.Assert(err, IsNil)
1045+
1046+
var docs []struct {
1047+
A string `bson:"a"`
1048+
}
1049+
err = coll.Find(bson.M{"a": "case"}).All(&docs)
1050+
c.Assert(err, IsNil)
1051+
c.Assert(docs[0].A, Equals, "case")
1052+
c.Assert(docs[1].A, Equals, "CaSe")
1053+
1054+
}
1055+
10231056
func (s *S) TestIsDupValues(c *C) {
10241057
c.Assert(mgo.IsDup(nil), Equals, false)
10251058
c.Assert(mgo.IsDup(&mgo.LastError{Code: 1}), Equals, false)

0 commit comments

Comments
 (0)