Skip to content

Commit 03207bb

Browse files
GODRIVER-2287 Add section to troubleshooting FAQ per driver with top SEO results (#1017)
Co-authored-by: Benjamin Rewis <[email protected]> Co-authored-by: Benjamin Rewis <[email protected]> Co-authored-by: Benjamin Rewis <[email protected]> Co-authored-by: Benjamin Rewis <[email protected]>
1 parent 063989d commit 03207bb

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,18 @@ Ensure the [`--networkMessageCompressors` flag](https://www.mongodb.com/docs/man
227227
-------------------------
228228
## Contribution
229229

230-
Check out the [project page](https://jira.mongodb.org/browse/GODRIVER) for tickets that need completing. See our [contribution guidelines](CONTRIBUTING.md) for details.
230+
Check out the [project page](https://jira.mongodb.org/browse/GODRIVER) for tickets that need completing. See our [contribution guidelines](docs/CONTRIBUTING.md) for details.
231231

232232
-------------------------
233233
## Continuous Integration
234234

235235
Commits to master are run automatically on [evergreen](https://evergreen.mongodb.com/waterfall/mongo-go-driver).
236236

237+
-------------------------
238+
## Frequently Encountered Issues
239+
240+
See our [common issues](docs/common-issues.md) documentation for troubleshooting frequently encountered issues.
241+
237242
-------------------------
238243
## Thanks and Acknowledgement
239244

File renamed without changes.

docs/common-issues.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Frequently Encountered Issues
2+
3+
## `WriteXXX` can only write while positioned on a Element or Value but is positioned on a TopLevel
4+
5+
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
6+
7+
> `WriteXXX` can only write while positioned on a Element or Value but is positioned on a TopLevel
8+
9+
occurs when the input to `bson.Marshal` is something *other* than a BSON Document. Examples of this occurrence include
10+
11+
- `WriteString`: the input into `bson.Marshal` is a string
12+
- `WriteNull`: the input into `bson.Marshal` is null
13+
- `WriteInt32`: the input into `bson.Marshal` is an integer
14+
15+
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:
16+
17+
```go
18+
var sort bson.D // this is nil and will result in a WriteNull error
19+
opts := options.FindOneAndUpdate().SetSort(sort)
20+
update := bson.D{{"$inc", bson.D{{"x", 1}}}}
21+
sr := coll.FindOneAndUpdate(ctx, bson.D{}, update)
22+
if err := sr.Err(); err != nil {
23+
log.Fatalf("error getting single result: %v", err)
24+
}
25+
```
26+
27+
The above example is resolved by initializing the `sort` variable:
28+
29+
```go
30+
sort := bson.D{}
31+
```
32+
33+
## Convert BSON Document to JSON
34+
35+
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):
36+
37+
```go
38+
doc := bson.D{{"x", 1}}
39+
40+
jsonBytes, err := bson.MarshalExtJSON(doc, true, false)
41+
if err != nil {
42+
log.Fatalf("error encoding json: %v", err)
43+
}
44+
45+
m := make(map[string]interface{})
46+
if err := json.Unmarshal(jsonBytes, &m); err != nil {
47+
log.Fatalf("error decoding json: %v", err)
48+
}
49+
fmt.Printf("json: %v\n", m)
50+
```
51+
52+

0 commit comments

Comments
 (0)