Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-3072

wrong doc for go driver in indexing

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Gone away
    • Icon: Minor - P4 Minor - P4
    • None
    • 1.13.1
    • None
    • None

    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)

      ```

      Attachments

        Activity

          People

            preston.vasquez@mongodb.com Preston Vasquez
            alirezaarzehgar82@gmail.com Alireza Arzehgar
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: