@@ -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+ //
30423074func (q * Query ) Collation (collation * Collation ) * Query {
30433075 q .m .Lock ()
30443076 q .op .options .Collation = collation
0 commit comments