...

Text file src/go.mongodb.org/mongo-driver/docs/common-issues.md

Documentation: go.mongodb.org/mongo-driver/docs

     1# Frequently Encountered Issues
     2
     3These are fixes or information for common issues encountered by Go Driver users. If none of these are helpful, [create a GODRIVER Jira ticket](https://jira.mongodb.org/secure/CreateIssue!default.jspa) and we'll try to troubleshoot your issue!
     4
     5## `WriteXXX` can only write while positioned on a Element or Value but is positioned on a TopLevel
     6
     7The [`bson.Marshal`](https://pkg.go.dev/go.mongodb.org/mongo-driver/bson#Marshal) function requires a parameter that can be decoded into a BSON Document, i.e. a [`primitive.D`](https://github.com/mongodb/mongo-go-driver/blob/master/bson/bson.go#L31). Therefore the error message
     8
     9> `WriteXXX` can only write while positioned on a Element or Value but is positioned on a TopLevel
    10
    11occurs when the input to `bson.Marshal` is something *other* than a BSON Document. Examples of this occurrence include
    12
    13- `WriteString`: the input into `bson.Marshal` is a string
    14- `WriteNull`: the input into `bson.Marshal` is null
    15- `WriteInt32`: the input into `bson.Marshal` is an integer
    16
    17Many CRUD operations in the Go Driver use `bson.Marshal` under the hood, so it's possible to encounter this particular error without directly attempting to encode data. For example, when using a sort on [`FindOneAndUpdate`](https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection.FindOneAndUpdate) this error can occur when not properly initializing the `sort` variable:
    18
    19```go
    20var sort bson.D // this is nil and will result in a WriteNull error
    21opts := options.FindOneAndUpdate().SetSort(sort)
    22update := bson.D{{"$inc", bson.D{{"x", 1}}}}
    23sr := coll.FindOneAndUpdate(ctx, bson.D{}, update)
    24if err := sr.Err(); err != nil {
    25	log.Fatalf("error getting single result: %v", err)
    26}
    27```
    28
    29The above example is resolved by initializing the `sort` variable:
    30
    31```go
    32sort := bson.D{}
    33```
    34
    35## Convert BSON Document to JSON
    36
    37There are a variety of marshalers that can be used to encode a BSON document as JSON, including [MarshalExtJSON](https://pkg.go.dev/github.com/mongodb/mongo-go-driver/bson#MarshalExtJSON):
    38
    39```go
    40doc := bson.D{{"x", 1}}
    41
    42jsonBytes, err := bson.MarshalExtJSON(doc, true, false)
    43if err != nil {
    44	log.Fatalf("error encoding json: %v", err)
    45}
    46
    47m := make(map[string]interface{})
    48if err := json.Unmarshal(jsonBytes, &m); err != nil {
    49	log.Fatalf("error decoding json: %v", err)
    50}
    51fmt.Printf("json: %v\n", m)
    52```
    53
    54## Authentication Failed
    55
    56When connecting to a MongoDB deployment with password authentication enabled, if your authentication information or configuration are incorrect, you may encounter an error message like:
    57
    58> connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
    59
    60That error can be caused by a number of issues. The error message intentionally omits the exact authentication failure reason (see the [OWASP Authentication and Error Messages guidelines](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#authentication-and-error-messages) for an explanation). Possible causes of the error include:
    61
    62- Incorrect password.
    63- Incorrect username.
    64- Incorrect authentication database (i.e. [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource)).
    65
    66If you encounter an `AuthenticationFailed` error like the one above, check that the username and password in your [connection string](https://www.mongodb.com/docs/manual/reference/connection-string/) or [SetAuth](https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo/options#ClientOptions.SetAuth) call are correct. In most cases, [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource) does not need to be specified unless you are using a non-default authentication database (the default is "admin").

View as plain text