Details
-
New Feature
-
Resolution: Gone away
-
Minor - P4
-
None
-
None
-
None
-
None
Description
Requesting a similar function to Map() or an option that enables Map() to recursively convert bson.D to a map.
Due to ordering, bson.D is preferable to bson.M and we're using bson.D exclusively in the developing Go documentation. This type isn't ideal when demonstrating output to users since everything is prepended with "Key" and "Value".
The Map() function provides a workaround for simple documents but breaks down on documents with arrays of documents, as it doesn't convert them.
Example:
var result bson.D
|
err = coll.FindOne(context.TODO(), bson.D{{"title", "The Room"}}).Decode(&result) |
// end findOne |
if err != nil { |
if err == mongo.ErrNoDocuments { |
// This error means your query did not match any documents. |
return |
}
|
panic(err)
|
}
|
// use Map() to quickly convert bson.D to a map |
output, err := json.MarshalIndent(result.Map(), "", " ") |
if err != nil { |
panic(err)
|
}
|
fmt.Printf("%s\n", output) |
{
|
"imdb": [
|
{
|
"Key": "rating",
|
"Value": 3.5
|
},
|
{
|
"Key": "votes",
|
"Value": 25673
|
},
|
{
|
"Key": "id",
|
"Value": 368226
|
}
|
],
|
"languages": [
|
"English"
|
],
|
...
|
"title": "The Room",
|
}
|
Attachments
Issue Links
- related to
-
GODRIVER-2684 Deprecate "bson.D.Map"
-
- Closed
-