@@ -38,7 +38,7 @@ type IndexView struct {
38
38
// IndexModel contains information about an index.
39
39
type IndexModel struct {
40
40
Keys interface {}
41
- Options interface {}
41
+ Options * options. IndexOptions
42
42
}
43
43
44
44
// List returns a cursor iterating over all the indexes in the collection.
@@ -100,11 +100,12 @@ func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ..
100
100
}
101
101
index := bsonx.Doc {{"key" , bsonx .Document (keys )}}
102
102
if model .Options != nil {
103
- options , err := transformDocument ( iv .coll . registry , model .Options )
103
+ optsDoc , err := iv .createOptionsDoc ( model .Options )
104
104
if err != nil {
105
105
return nil , err
106
106
}
107
- index = append (index , options ... )
107
+
108
+ index = append (index , optsDoc ... )
108
109
}
109
110
index = index .Set ("name" , bsonx .String (name ))
110
111
@@ -140,6 +141,82 @@ func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ..
140
141
return names , nil
141
142
}
142
143
144
+ func (iv IndexView ) createOptionsDoc (opts * options.IndexOptions ) (bsonx.Doc , error ) {
145
+ optsDoc := bsonx.Doc {}
146
+ if opts .Background != nil {
147
+ optsDoc = append (optsDoc , bsonx.Elem {"background" , bsonx .Boolean (* opts .Background )})
148
+ }
149
+ if opts .ExpireAfterSeconds != nil {
150
+ optsDoc = append (optsDoc , bsonx.Elem {"expireAfterSeconds" , bsonx .Int32 (* opts .ExpireAfterSeconds )})
151
+ }
152
+ if opts .Name != nil {
153
+ optsDoc = append (optsDoc , bsonx.Elem {"name" , bsonx .String (* opts .Name )})
154
+ }
155
+ if opts .Sparse != nil {
156
+ optsDoc = append (optsDoc , bsonx.Elem {"sparse" , bsonx .Boolean (* opts .Sparse )})
157
+ }
158
+ if opts .StorageEngine != nil {
159
+ doc , err := transformDocument (iv .coll .registry , opts .StorageEngine )
160
+ if err != nil {
161
+ return nil , err
162
+ }
163
+
164
+ optsDoc = append (optsDoc , bsonx.Elem {"storageEngine" , bsonx .Document (doc )})
165
+ }
166
+ if opts .Unique != nil {
167
+ optsDoc = append (optsDoc , bsonx.Elem {"unique" , bsonx .Boolean (* opts .Unique )})
168
+ }
169
+ if opts .Version != nil {
170
+ optsDoc = append (optsDoc , bsonx.Elem {"v" , bsonx .Int32 (* opts .Version )})
171
+ }
172
+ if opts .DefaultLanguage != nil {
173
+ optsDoc = append (optsDoc , bsonx.Elem {"default_language" , bsonx .String (* opts .DefaultLanguage )})
174
+ }
175
+ if opts .LanguageOverride != nil {
176
+ optsDoc = append (optsDoc , bsonx.Elem {"language_override" , bsonx .String (* opts .LanguageOverride )})
177
+ }
178
+ if opts .TextVersion != nil {
179
+ optsDoc = append (optsDoc , bsonx.Elem {"textIndexVersion" , bsonx .Int32 (* opts .TextVersion )})
180
+ }
181
+ if opts .Weights != nil {
182
+ weightsDoc , err := transformDocument (iv .coll .registry , opts .Weights )
183
+ if err != nil {
184
+ return nil , err
185
+ }
186
+
187
+ optsDoc = append (optsDoc , bsonx.Elem {"weights" , bsonx .Document (weightsDoc )})
188
+ }
189
+ if opts .SphereVersion != nil {
190
+ optsDoc = append (optsDoc , bsonx.Elem {"2dsphereIndexVersion" , bsonx .Int32 (* opts .SphereVersion )})
191
+ }
192
+ if opts .Bits != nil {
193
+ optsDoc = append (optsDoc , bsonx.Elem {"bits" , bsonx .Int32 (* opts .Bits )})
194
+ }
195
+ if opts .Max != nil {
196
+ optsDoc = append (optsDoc , bsonx.Elem {"max" , bsonx .Double (* opts .Max )})
197
+ }
198
+ if opts .Min != nil {
199
+ optsDoc = append (optsDoc , bsonx.Elem {"min" , bsonx .Double (* opts .Min )})
200
+ }
201
+ if opts .BucketSize != nil {
202
+ optsDoc = append (optsDoc , bsonx.Elem {"bucketSize" , bsonx .Int32 (* opts .BucketSize )})
203
+ }
204
+ if opts .PartialFilterExpression != nil {
205
+ doc , err := transformDocument (iv .coll .registry , opts .PartialFilterExpression )
206
+ if err != nil {
207
+ return nil , err
208
+ }
209
+
210
+ optsDoc = append (optsDoc , bsonx.Elem {"partialFilterExpression" , bsonx .Document (doc )})
211
+ }
212
+ if opts .Collation != nil {
213
+ doc := opts .Collation .ToDocument ()
214
+ optsDoc = append (optsDoc , bsonx.Elem {"collation" , bsonx .Document (doc )})
215
+ }
216
+
217
+ return optsDoc , nil
218
+ }
219
+
143
220
// DropOne drops the index with the given name from the collection.
144
221
func (iv IndexView ) DropOne (ctx context.Context , name string , opts ... * options.DropIndexesOptions ) (bson.Raw , error ) {
145
222
if name == "*" {
@@ -197,25 +274,8 @@ func (iv IndexView) DropAll(ctx context.Context, opts ...*options.DropIndexesOpt
197
274
}
198
275
199
276
func getOrGenerateIndexName (registry * bsoncodec.Registry , model IndexModel ) (string , error ) {
200
- if model .Options != nil {
201
- options , err := transformDocument (registry , model .Options )
202
- if err != nil {
203
- return "" , err
204
- }
205
- nameVal , err := options .LookupErr ("name" )
206
-
207
- switch err .(type ) {
208
- case bsonx.KeyNotFound :
209
- break
210
- case nil :
211
- if nameVal .Type () != bson .TypeString {
212
- return "" , ErrNonStringIndexName
213
- }
214
-
215
- return nameVal .StringValue (), nil
216
- default :
217
- return "" , err
218
- }
277
+ if model .Options != nil && model .Options .Name != nil {
278
+ return * model .Options .Name , nil
219
279
}
220
280
221
281
name := bytes .NewBufferString ("" )
0 commit comments