[GODRIVER-3072] wrong doc for go driver in indexing Created: 15/Dec/23  Updated: 23/Jan/24  Resolved: 23/Jan/24

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

Type: Task Priority: Minor - P4
Reporter: Alireza Arzehgar Assignee: Preston Vasquez
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented

 Description   

I'm not an expert. But I think we have some problems on documentions of go driver.

I will be glad if I can contribute to solving these problems.

 

Anyway this is explanation of a problem:

 

I read this document page for using indexes.

 

Code examples have error. for example see following code:
 
```go

indexModel := mongo.IndexModel{
    Keys: bson.D{{"title", 1}}
}
name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
if err != nil

{     panic(err) }

fmt.Println("Name of Index Created: " + name)

````

First bug of this code is that in line 2 we should write a comma. This code wont compile.

 

Next problem is that following code will throw error:

```go

func main() {
    client, err := mongo.Connect(
        context.Background(),

        options.Client().ApplyURI("mongodb://127.0.0.1:27017"),
    )

    database := client.Database("appdb")
    uc := database.Collection("users")

    indexModel := mongo.IndexModel{
        Keys:    bson.D{{"username", 1}},
        Options: options.Index().SetUnique(true),
    }
    name, err := uc.Indexes().CreateOne(context.TODO(), indexModel)
    if err != nil

{         log.Fatal(err)     }

    fmt.Println("Name of Index Created: " + name)
}

```

 

This code will print following error:

```

2023/12/15 23:18:50 cannot marshal type bson.D to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel

```

If I change IndexModel.keys type to []string or bson.M, code will work correctly.

Correct code:

```go

    indexModel := mongo.IndexModel

{         Keys:    bson.M\{"username": 1}

,
        Options: options.Index().SetUnique(true),
    }
    name, err := uc.Indexes().CreateOne(context.TODO(), indexModel)

```

 

Correct code:

```

    indexModel := mongo.IndexModel

{         Keys:    []string\{"username"}

,
        Options: options.Index().SetUnique(true),
    }
    name, err := uc.Indexes().CreateOne(context.TODO(), indexModel)

```



 Comments   
Comment by PM Bot [ 23/Jan/24 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to reopen this ticket if you're still experiencing the issue, and add a comment if you're able to provide more information.

Comment by PM Bot [ 16/Jan/24 ]

Hi alirezaarzehgar82@gmail.com! GODRIVER-3072 is awaiting your response.

If this is still an issue for you, please open Jira to review the latest status and provide your feedback. Thanks!

Comment by Preston Vasquez [ 09/Jan/24 ]

Hi alirezaarzehgar82@gmail.com , you are correct there appears to be a typo in the documentation. Thank you for pointing this out, I have created DOCS-16571 to address this issue.

The code you've provided compiles and runs without error on 1.13.1:

package main
 
import (
	"context"
	"fmt"
	"log"
 
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)
 
func main() {
	client, err := mongo.Connect(
		context.Background(),
 
		options.Client().ApplyURI("mongodb://127.0.0.1:27017"),
	)
 
	database := client.Database("appdb")
	uc := database.Collection("users")
 
	indexModel := mongo.IndexModel{
		Keys:    bson.D{{"username", 1}},
		Options: options.Index().SetUnique(true),
	}
 
	name, err := uc.Indexes().CreateOne(context.TODO(), indexModel)
	if err != nil {
		log.Fatal(err)
	}
 
	fmt.Println("Name of Index Created: " + name)
}

Could you give us more information on your use case? Note that the following will not run successfully:

	indexModel := mongo.IndexModel{
		Keys:    []string{"username"},
		Options: options.Index().SetUnique(true),
	}

And results in the following error:

2024/01/08 17:02:07 cannot marshal type []string to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel

As documented by the Go Driver, Keys must be an order-preserving type, such as bson.D. While bson.M does not error like a slice would, it represents incorrect usage and will result in unexpected behavior.

Comment by PM Bot [ 15/Dec/23 ]

Hi alirezaarzehgar82@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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