@@ -1058,30 +1058,32 @@ func (db *Database) RemoveUser(user string) error {
10581058}
10591059
10601060type indexSpec struct {
1061- Name , NS string
1062- Key bson.D
1063- Unique bool ",omitempty"
1064- DropDups bool "dropDups,omitempty"
1065- Background bool ",omitempty"
1066- Sparse bool ",omitempty"
1067- Bits int ",omitempty"
1068- Min , Max float64 ",omitempty"
1069- BucketSize float64 "bucketSize,omitempty"
1070- ExpireAfter int "expireAfterSeconds,omitempty"
1071- Weights bson.D ",omitempty"
1072- DefaultLanguage string "default_language,omitempty"
1073- LanguageOverride string "language_override,omitempty"
1074- TextIndexVersion int "textIndexVersion,omitempty"
1061+ Name , NS string
1062+ Key bson.D
1063+ Unique bool ",omitempty"
1064+ DropDups bool "dropDups,omitempty"
1065+ Background bool ",omitempty"
1066+ Sparse bool ",omitempty"
1067+ Bits int ",omitempty"
1068+ Min , Max float64 ",omitempty"
1069+ BucketSize float64 "bucketSize,omitempty"
1070+ ExpireAfter int "expireAfterSeconds,omitempty"
1071+ Weights bson.D ",omitempty"
1072+ DefaultLanguage string "default_language,omitempty"
1073+ LanguageOverride string "language_override,omitempty"
1074+ TextIndexVersion int "textIndexVersion,omitempty"
1075+ PartialFilterExpression bson.M "partialFilterExpression,omitempty"
10751076
10761077 Collation * Collation "collation,omitempty"
10771078}
10781079
10791080type Index struct {
1080- Key []string // Index key fields; prefix name with dash (-) for descending order
1081- Unique bool // Prevent two documents from having the same index key
1082- DropDups bool // Drop documents with the same index key as a previously indexed one
1083- Background bool // Build index in background and return immediately
1084- Sparse bool // Only index documents containing the Key fields
1081+ Key []string // Index key fields; prefix name with dash (-) for descending order
1082+ Unique bool // Prevent two documents from having the same index key
1083+ DropDups bool // Drop documents with the same index key as a previously indexed one
1084+ Background bool // Build index in background and return immediately
1085+ Sparse bool // Only index documents containing the Key fields
1086+ PartialFilter bson.M // Partial index filter expression
10851087
10861088 // If ExpireAfter is defined the server will periodically delete
10871089 // documents with indexed time.Time older than the provided delta.
@@ -1334,6 +1336,10 @@ func (c *Collection) EnsureIndexKey(key ...string) error {
13341336// http://www.mongodb.org/display/DOCS/Multikeys
13351337//
13361338func (c * Collection ) EnsureIndex (index Index ) error {
1339+ if index .Sparse && index .PartialFilter != nil {
1340+ return errors .New ("cannot mix sparse and partial indexes" )
1341+ }
1342+
13371343 keyInfo , err := parseIndexKey (index .Key )
13381344 if err != nil {
13391345 return err
@@ -1346,22 +1352,23 @@ func (c *Collection) EnsureIndex(index Index) error {
13461352 }
13471353
13481354 spec := indexSpec {
1349- Name : keyInfo .name ,
1350- NS : c .FullName ,
1351- Key : keyInfo .key ,
1352- Unique : index .Unique ,
1353- DropDups : index .DropDups ,
1354- Background : index .Background ,
1355- Sparse : index .Sparse ,
1356- Bits : index .Bits ,
1357- Min : index .Minf ,
1358- Max : index .Maxf ,
1359- BucketSize : index .BucketSize ,
1360- ExpireAfter : int (index .ExpireAfter / time .Second ),
1361- Weights : keyInfo .weights ,
1362- DefaultLanguage : index .DefaultLanguage ,
1363- LanguageOverride : index .LanguageOverride ,
1364- Collation : index .Collation ,
1355+ Name : keyInfo .name ,
1356+ NS : c .FullName ,
1357+ Key : keyInfo .key ,
1358+ Unique : index .Unique ,
1359+ DropDups : index .DropDups ,
1360+ Background : index .Background ,
1361+ Sparse : index .Sparse ,
1362+ Bits : index .Bits ,
1363+ Min : index .Minf ,
1364+ Max : index .Maxf ,
1365+ BucketSize : index .BucketSize ,
1366+ ExpireAfter : int (index .ExpireAfter / time .Second ),
1367+ Weights : keyInfo .weights ,
1368+ DefaultLanguage : index .DefaultLanguage ,
1369+ LanguageOverride : index .LanguageOverride ,
1370+ Collation : index .Collation ,
1371+ PartialFilterExpression : index .PartialFilter ,
13651372 }
13661373
13671374 if spec .Min == 0 && spec .Max == 0 {
@@ -1577,6 +1584,7 @@ func indexFromSpec(spec indexSpec) Index {
15771584 LanguageOverride : spec .LanguageOverride ,
15781585 ExpireAfter : time .Duration (spec .ExpireAfter ) * time .Second ,
15791586 Collation : spec .Collation ,
1587+ PartialFilter : spec .PartialFilterExpression ,
15801588 }
15811589 if float64 (int (spec .Min )) == spec .Min && float64 (int (spec .Max )) == spec .Max {
15821590 index .Min = int (spec .Min )
0 commit comments