• go中bson的基本操作,bson.M,bson.D,bson.A,bson.E


    欢迎关注公众号:天天说编程

    你的关注是我最大的动力!

    1.Bson的类型

    bson对象是键值对对象,bson是JSON的二进制格式。go操作mongoDB数据库的时候经常使用bson键值对作为筛选条件。

    D家族,可以简单的构建BSON对象。

    D:一个BSON文档,这种类型应该在顺序重要的情况下使用。 每一对键值对都包含一个大括号,bson.D{{key,value},{key,value}},中间用逗号连接key,value。

    M:一个无序的map,它和D是一样的,只是它不保持顺序。 每一对键值对不使用大括号,bson.M{key:value},中间用 冒号key:value 进行连接。

    A:一个BSON数组,当使用“$and”,“$or”等要使用数组。

    E:D里面的一个元素。

    1. package bson // import "go.mongodb.org/mongo-driver/bson"
    2. import (
    3. "go.mongodb.org/mongo-driver/bson/primitive"
    4. )
    5. // D is an ordered representation of a BSON document. This type should be used when the order of the elements matters,
    6. // such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
    7. // Example usage:
    8. // bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
    9. type D = primitive.D
    10. // E represents a BSON element for a D. It is usually used inside a D.
    11. type E = primitive.E
    12. // M is an unordered representation of a BSON document. This type should be used when the order of the elements does not
    13. // Example usage:
    14. //
    15. // bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
    16. type M = primitive.M
    17. // An A is an ordered representation of a BSON array.
    18. // Example usage:
    19. // bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
    20. type A = primitive.A

    (1)bson.D

    bson.D是一种有序的键值对,D(Document)格式代表了一个BSON文档。D包含了一个E切片,

    type D []E

    每一对键值对都需要用大括号来连接,bson.D{{key,value},{key,value}...},键和值之间使用逗号连接,比如:

    bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}

    (2)bson.E

    bson.D有序键值对的其中一个元素。一个bson.D由多个bson.E构成。

    1. // E represents a BSON element for a D. It is usually used inside a D.
    2. type E struct {
    3. Key string
    4. Value interface{}
    5. }
    6. bson.D{
    7. bson.E{Key: "name", Value: "zhangsan"},
    8. bson.E{Key: "age", Value: 19},
    9. }

    (3)bson.M

    bson.M是无序的键值对,键值对之间使用冒号连接,比如:

    bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}

    (4)bson.A

    bson.A是一个bson数组,当使用“$and”,等等,会使用数组。比如:

    1. a := bson.A{
    2. "bar",
    3. "cars",
    4. bson.M{
    5. "name": "zhangsan",
    6. "age": 18,
    7. },
    8. bson.E{
    9. Key: "address",
    10. Value: "China",
    11. },
    12. bson.D{
    13. {"cinder", "kingsoft"},
    14. {"glance", "kingsoft"},
    15. },
    16. }

    bson.D和bson.M区别

    ①bson.D是有序的,bson.M是无序的。

    ②bson.D的键值对需要大括号括起来,bson.M不需要

    ③bson.M键值对使用:链接,bson.D键值对使用逗号,连接

    2.举例

    1. func bsonTestM() {
    2. docs := bson.M{
    3. "name": "Alice",
    4. "age": 18,
    5. // addr是一个无序的键值对
    6. "addr": bson.M{
    7. "city": "beijing",
    8. "country": "China",
    9. },
    10. }
    11. // 序列化字节数组 func Marshal(val interface{}) ([]byte, error) {
    12. data, err := bson.Marshal(docs)
    13. if err != nil {
    14. panic(err)
    15. }
    16. fmt.Println("输出序列化的data内容:", data)
    17. var result bson.M
    18. // 方便将字节[]byte映射到结果集中。
    19. err = bson.Unmarshal(data, &result)
    20. if err != nil {
    21. panic(err)
    22. }
    23. fmt.Println("输出反序列化后的内容:", result)
    24. fmt.Printf("bson.M类型为%T", bson.M{})
    25. }
    26. // 有序的键值对对象
    27. func bsonTestD() {
    28. docs := bson.D{
    29. {"name", "zhangsan"},
    30. {"age", 18},
    31. }
    32. bytes, err := bson.Marshal(docs)
    33. if err != nil {
    34. panic(err)
    35. }
    36. var result bson.D
    37. // 为什么使用&result
    38. // 因为Unmarshal函数需要修改目标对象的值,而不仅仅是复制数据。通过传递指针,函数可以直接在目标对象的内存地址上进行操作,
    39. // 从而避免了对整个对象进行复制,节省了内存和处理时间。
    40. //另外,通过传递指针,可以确保Unmarshal函数可以正确地更新目标对象的值,而不是创建一个新的对象。
    41. /*
    42. 总之,&result,其实就是为了可以直接在目标对象的内存地址进行操作,避免对整个对象的复制,节省了内存和处理时间。
    43. */
    44. err = bson.Unmarshal(bytes, &result)
    45. if err != nil {
    46. panic(err)
    47. }
    48. fmt.Println(result)
    49. }
    50. func testBsonA() {
    51. a := bson.A{
    52. "bar",
    53. "cars",
    54. bson.M{
    55. "name": "zhangsan",
    56. "age": 18,
    57. },
    58. bson.E{
    59. Key: "address",
    60. Value: "China",
    61. },
    62. bson.D{
    63. {"cinder", "cinder"},
    64. {"glance", "demo"},
    65. },
    66. }
    67. fmt.Println(a)
    68. }

    其实用的比较多的是在go操作mongoDB的时候,因为mongoDB是键值对的数据库,bson文档可以作为filter过滤条件,这个用的多一些。

    欢迎关注公众号:天天说编程

    你的关注是我最大的动力!

  • 相关阅读:
    5-2Web应用程序漏洞扫描
    【算法】斐波那契数列与台风的故事
    Flink - 读取 Parquet 文件 By Scala / Java
    Java刷题面试系列习题(五)
    P3371 【模板】单源最短路径(弱化版)
    Mybatis框架_涉及技术与拓展
    DevOps-4:Jenkins配置.Net项目模板Job
    物联网视觉处理-opencv在win下安装及应用
    Flask(Jinja2) 服务端模板注入漏洞(SSTI)
    【动手学深度学习】--语言模型
  • 原文地址:https://blog.csdn.net/Sunshineoe/article/details/133668839