[GODRIVER-1122] Index creation failed when the index name is empty Created: 11/Jun/19  Updated: 27/Oct/23  Resolved: 29/Jul/19

Status: Closed
Project: Go Driver
Component/s: API
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Tiansheng Ren Assignee: Isabella Siu (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2019-07-04-16-07-01-910.png    

 Description   

mongo client support create index allow index name emtpy.

but driver return "index name cannot be empty"



 Comments   
Comment by Isabella Siu (Inactive) [ 08/Jul/19 ]

Hi reage,

If name is an empty string and you want to create an index, you should leave IndexOptions.Name nil. If IndexOptions.Name is nil the driver will generate an index name for you.

name := ""    // parent function transfer value
indexOpts := options.Index()
if name != "" {
    indexOpts.SetName(name)
}

Comment by Tiansheng Ren [ 04/Jul/19 ]

package remote
import (    
    "context"   
    "strconv"  
    "testing" 
   "time"
 
    "github.com/mongodb/mongo-go-driver/mongo" 
   "github.com/mongodb/mongo-go-driver/mongo/options"
    "github.com/mongodb/mongo-go-driver/x/bsonx"
    "github.com/stretchr/testify/require"
)
 
func TestCreateIndex(t *testing.T) {   
    uri := "mongodb://test:test@127.0.0.1:27017/test?authMechanism=SCRAM-SHA-1"
    mc, err := mongo.NewClient(uri) 
    require.NoError(t, err)
    err = mc.Connect(context.Background())
    require.NoError(t, err)
    err = mc.Ping(nil, nil)
    require.NoError(t, err)
 
    indexView := mc.Database("cmdb").Collection("tmptest").Indexes()
    keys := bsonx.Doc{}
    keys = keys.Append(strconv.FormatInt(time.Now().Unix(), 10), bsonx.Int32(1))
    name := ""    // parent function transfer value
    indexOpts := &options.IndexOptions{
        Name:       &name,
        Background: nil,
        Unique:     nil,   
     }
     _, err = indexView.CreateOne( 
        context.TODO(),
        mongo.IndexModel{  Keys: keys,  Options: indexOpts},  
    )
    require.NoError(t, err)
}

 

 

 

Comment by Isabella Siu (Inactive) [ 17/Jun/19 ]

Hi reage,

Can you give us an example of code that produces this error?

Comment by Tiansheng Ren [ 11/Jun/19 ]

mongodb  v4.1.13 use replica-set 

 

mongodb driver version:  v1.0.0-rc1+prerelease

Generated at Thu Feb 08 08:35:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.