-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
Use a different syntax for declaring BSON document literals to avoid the "unkeyed fields" "go vet" linter warnings.
Reference BSON document definitions
JSON:
{ "key1": { "nested": "value" }, "key2": "value2", "key3": { "nested1": { "nested2": 14 } } }
Javascript:
{ key1: { nested: "value", }, key2: "value2", key3: { nested1: { nested2: 14, }, }, }
Existing Go syntax
bson.M:
fmt.Println(bson.M{ "key1": bson.M{ "nested": "value", }, "key2": "value2", "key3": bson.M{ "nested1": bson.M{ "nested2": 14, }, }, })
bson.D:
fmt.Println(bson.D{ {"key1", bson.D{ {"nested", "value"}, }}, {"key2", "value2"}, {"key3", bson.D{ {"nested1", bson.D{ {"nested2", 14}, }}, }}, })
bson.D with keyed fields:
fmt.Println(bson.D{ {Key: "key1", Value: bson.D{ {Key: "nested", Value: "value"}, }}, {Key: "key2", Value: "value2"}, {Key: "key3", Value: bson.D{ {Key: "nested1", Value: bson.D{ {Key: "nested2", Value: 14}, }}, }}, })
bson.D syntax proposals
- bson.D with keyed parameters and short keys:
fmt.Println(bson.D{ {K: "key1", V: bson.D{ {K: "nested", V: "value"}, }}, {K: "key2", V: "value2"}, {K: "key3", V: bson.D{ {K: "nested1", V: bson.D{ {K: "nested2", V: 14}, }}, }}, })
- bson.D with bson.E as a function:
fmt.Println(bson.D{ bson.E("key1", bson.D{ bson.E("nested", "value"), }), bson.E("key2", "value2"), bson.E("key3", bson.D{ bson.E("nested1", bson.D{ bson.E("nested2", 14), }), }), })
- bson.D and bson.E as functions:
fmt.Println(bson.D( bson.E("key1", bson.D( bson.E("nested", "value"), )), bson.E("key2", "value2"), bson.E("key3", bson.D( bson.E("nested1", bson.D( bson.E("nested2", 14), )), )), ))
- bson.D as function that takes an even number of k/v arguments:
fmt.Println(bson.D( "key1", bson.D( "nested", "value", ), "key2", "value2", "key3", bson.D( "nested1", bson.D( "nested2", 14, ), ), ))
- bson.D as the builder pattern:
fmt.Println(bson.D(). E("key1", bson.D(). E("nested", "value"), ). E("key2", "value2"). E("key3", bson.D(). E("nested1", bson.D(). E("nested2", 14), ), ), )
- JSON-to-BSON converter function (from GODRIVER-2459).
fmt.Println(mongo.MustCompileQuery(`{ "key1": { "nested": $1 }, "key2": $2, "key3": { "nested1": { "nested2": $3 } }`, "$1", "value", "$2", "value2", "$3", 14, ))
- is related to
-
GODRIVER-2889 Create aggregation pipeline helpers
- Backlog
- related to
-
GODRIVER-2459 Proposal: Improve a way of making filters and aggregation pipilines
- Backlog