Skip to content

Commit c1b9339

Browse files
author
Isabella Siu
committed
GODRIVER-856 make chunks collection index _id_1_n_1 unique
Change-Id: I0d5cbf569f6da99c0a1d296dc64341b567759d62
1 parent 60e1c81 commit c1b9339

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

bson/primitive/primitive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Undefined struct{}
3535
// DateTime represents the BSON datetime value.
3636
type DateTime int64
3737

38-
// MarshalJSON marshal to time type
38+
// MarshalJSON marshal to time type
3939
func (d DateTime) MarshalJSON() ([]byte, error) {
4040
return json.Marshal(time.Unix(int64(d)/1000, int64(d)%1000*1000000))
4141
}

mongo/gridfs/bucket.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ func (b *Bucket) createIndexes(ctx context.Context) error {
490490
{"files_id", int32(1)},
491491
{"n", int32(1)},
492492
},
493+
Options: options.Index().SetUnique(true),
493494
}
494495

495496
if err = createIndexIfNotExists(ctx, filesIv, filesModel); err != nil {

mongo/gridfs/gridfs_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ var chunkSizeTests = []struct {
3939
{"Bucket and upload set to different values", options.GridFSBucket().SetChunkSizeBytes(27), options.GridFSUpload().SetChunkSizeBytes(31)},
4040
}
4141

42-
func findIndex(ctx context.Context, t *testing.T, coll *mongo.Collection, keys ...string) {
42+
func findIndex(ctx context.Context, t *testing.T, coll *mongo.Collection, unique bool, keys ...string) {
4343
cur, err := coll.Indexes().List(ctx)
4444
if err != nil {
4545
t.Fatalf("Couldn't establish a cursor on the collection %v: %v", coll.Name(), err)
4646
}
4747
foundIndex := false
4848
for cur.Next(ctx) {
4949
if _, err := cur.Current.LookupErr(keys...); err == nil {
50-
foundIndex = true
50+
if uVal, err := cur.Current.LookupErr("unique"); (unique && err == nil && uVal.Boolean() == true) || (!unique && (err != nil || uVal.Boolean() == false)) {
51+
foundIndex = true
52+
}
5153
}
5254
}
5355
if !foundIndex {
@@ -183,8 +185,8 @@ func TestGridFS(t *testing.T) {
183185
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
184186
defer cancel()
185187

186-
findIndex(findCtx, t, bucket.filesColl, "key", "filename")
187-
findIndex(findCtx, t, bucket.chunksColl, "key", "files_id")
188+
findIndex(findCtx, t, bucket.filesColl, false, "key", "filename")
189+
findIndex(findCtx, t, bucket.chunksColl, true, "key", "files_id")
188190
})
189191

190192
t.Run("RoundTrip", func(t *testing.T) {

0 commit comments

Comments
 (0)