Skip to content

Commit 8fe756a

Browse files
author
Divjot Arora
authored
GODRIVER-1600 Add support for hidden index option (#400)
1 parent ac7f51c commit 8fe756a

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

mongo/index_view.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ func (iv IndexView) createOptionsDoc(opts *options.IndexOptions) (bsoncore.Docum
334334

335335
optsDoc = bsoncore.AppendDocumentElement(optsDoc, "wildcardProjection", doc)
336336
}
337+
if opts.Hidden != nil {
338+
optsDoc = bsoncore.AppendBooleanElement(optsDoc, "hidden", *opts.Hidden)
339+
}
337340

338341
return optsDoc, nil
339342
}

mongo/integration/index_view_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ func TestIndexView(t *testing.T) {
136136
})
137137
})
138138
})
139+
mt.RunOpts("hidden", mtest.NewOptions().MinServerVersion("4.4"), func(mt *mtest.T) {
140+
iv := mt.Coll.Indexes()
141+
keysDoc := bson.D{{"x", int32(1)}}
142+
model := mongo.IndexModel{
143+
Keys: keysDoc,
144+
Options: options.Index().SetHidden(true),
145+
}
146+
147+
_, err := iv.CreateOne(mtest.Background, model)
148+
assert.Nil(mt, err, "CreateOne error: %v", err)
149+
150+
indexDoc := getIndexDoc(mt, iv, keysDoc)
151+
assert.NotNil(mt, indexDoc, "index with keys document %v was not found", keysDoc)
152+
checkIndexDocContains(mt, indexDoc, bson.E{
153+
Key: "hidden",
154+
Value: true,
155+
})
156+
})
139157
mt.Run("nil keys", func(mt *mtest.T) {
140158
_, err := mt.Coll.Indexes().CreateOne(mtest.Background, mongo.IndexModel{
141159
Keys: nil,

mongo/options/indexoptions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ type IndexOptions struct {
249249

250250
// A document that defines the wildcard projection for the index.
251251
WildcardProjection interface{}
252+
253+
// If true, the index will exist on the target collection but will not be used by the query planner when executing
254+
// operations. This option is only valid for MongoDB versions >= 4.4. The default value is false.
255+
Hidden *bool
252256
}
253257

254258
// Index creates a new IndexOptions instance.
@@ -370,6 +374,12 @@ func (i *IndexOptions) SetWildcardProjection(wildcardProjection interface{}) *In
370374
return i
371375
}
372376

377+
// SetHidden sets the value for the Hidden field.
378+
func (i *IndexOptions) SetHidden(hidden bool) *IndexOptions {
379+
i.Hidden = &hidden
380+
return i
381+
}
382+
373383
// MergeIndexOptions combines the given IndexOptions into a single IndexOptions in a last-one-wins fashion.
374384
func MergeIndexOptions(opts ...*IndexOptions) *IndexOptions {
375385
i := Index()
@@ -432,6 +442,9 @@ func MergeIndexOptions(opts ...*IndexOptions) *IndexOptions {
432442
if opt.WildcardProjection != nil {
433443
i.WildcardProjection = opt.WildcardProjection
434444
}
445+
if opt.Hidden != nil {
446+
i.Hidden = opt.Hidden
447+
}
435448
}
436449

437450
return i

0 commit comments

Comments
 (0)