-
Notifications
You must be signed in to change notification settings - Fork 913
GODRIVER-2287 Add section to troubleshooting FAQ per driver with top SEO results #1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1a96deb
GODRIVER-2287 add troubleshooting docs for WriteXXX error
prestonvasquez b275aa1
GODRIVER-2287 further clarify WriteXXX error
prestonvasquez a0e7a66
GODRIVER-2287 further clarify WriteXXX error
prestonvasquez c9bf031
GODRIVER-2287 further clarify WriteXXX error
prestonvasquez a152664
GODRIVEr-2287 more docs cleanup
prestonvasquez d58644d
GODRIVER-2287 add encoding docs
prestonvasquez 833a128
GODRIVER-2287 remove comments
prestonvasquez 623c76f
GODRIVER-2287 mv the CONTRIBUTING.md to /docs
prestonvasquez b48c919
GODRIVER-2287 update README.md links to point at docs
prestonvasquez 3962c9b
GODRIVER-2287 lowercase common issues
prestonvasquez e15f99e
GODRIVER-2297 fix bson document references
prestonvasquez 877c5ed
GODRIVER-2297 update BSON Document in section title
prestonvasquez 64e5c84
Update docs/common-issues.md
prestonvasquez d7f0508
Update docs/common-issues.md
prestonvasquez 0222a23
Update docs/common-issues.md
prestonvasquez 30e02f3
GODRIVER-2287 replace +v with v for map.
prestonvasquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Frequently Encountered Issues | ||
|
||
## `WriteXXX` can only write while positioned on a Element or Value but is positioned on a TopLevel | ||
|
||
The [`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 | ||
|
||
> `WriteXXX` can only write while positioned on a Element or Value but is positioned on a TopLevel | ||
|
||
occurs when the input to `bson.Marshal` is something *other* than a BSON Document. Examples of this occurrence include | ||
|
||
- `WriteString`: the input into `bson.Marshal` is a string | ||
- `WriteNull`: the input into `bson.Marshal` is null | ||
- `WriteInt32`: the input into `bson.Marshal` is an integer | ||
|
||
Many 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: | ||
|
||
```go | ||
var sort bson.D // this is nil and will result in a WriteNull error | ||
opts := options.FindOneAndUpdate().SetSort(sort) | ||
update := bson.D{{"$inc", bson.D{{"x", 1}}}} | ||
sr := coll.FindOneAndUpdate(ctx, bson.D{}, update) | ||
if err := sr.Err(); err != nil { | ||
log.Fatalf("error getting single result: %v", err) | ||
} | ||
``` | ||
|
||
The above example is resolved by initializing the `sort` variable: | ||
|
||
```go | ||
sort := bson.D{} | ||
``` | ||
|
||
## Convert BSON Document to JSON | ||
|
||
There 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): | ||
|
||
```go | ||
doc := bson.D{{"x", 1}} | ||
|
||
jsonBytes, err := bson.MarshalExtJSON(doc, true, false) | ||
if err != nil { | ||
log.Fatalf("error encoding json: %v", err) | ||
} | ||
|
||
m := make(map[string]interface{}) | ||
if err := json.Unmarshal(jsonBytes, &m); err != nil { | ||
log.Fatalf("error decoding json: %v", err) | ||
} | ||
fmt.Printf("json: %v\n", m) | ||
``` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.