@@ -1058,30 +1058,32 @@ func (db *Database) RemoveUser(user string) error {
1058
1058
}
1059
1059
1060
1060
type 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"
1075
1076
1076
1077
Collation * Collation "collation,omitempty"
1077
1078
}
1078
1079
1079
1080
type 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
1085
1087
1086
1088
// If ExpireAfter is defined the server will periodically delete
1087
1089
// documents with indexed time.Time older than the provided delta.
@@ -1334,6 +1336,10 @@ func (c *Collection) EnsureIndexKey(key ...string) error {
1334
1336
// http://www.mongodb.org/display/DOCS/Multikeys
1335
1337
//
1336
1338
func (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
+
1337
1343
keyInfo , err := parseIndexKey (index .Key )
1338
1344
if err != nil {
1339
1345
return err
@@ -1346,22 +1352,23 @@ func (c *Collection) EnsureIndex(index Index) error {
1346
1352
}
1347
1353
1348
1354
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 ,
1365
1372
}
1366
1373
1367
1374
if spec .Min == 0 && spec .Max == 0 {
@@ -1577,6 +1584,7 @@ func indexFromSpec(spec indexSpec) Index {
1577
1584
LanguageOverride : spec .LanguageOverride ,
1578
1585
ExpireAfter : time .Duration (spec .ExpireAfter ) * time .Second ,
1579
1586
Collation : spec .Collation ,
1587
+ PartialFilter : spec .PartialFilterExpression ,
1580
1588
}
1581
1589
if float64 (int (spec .Min )) == spec .Min && float64 (int (spec .Max )) == spec .Max {
1582
1590
index .Min = int (spec .Min )
0 commit comments