Go MongoDB Driver 中的 A D M E 类型是什么

先看看源码

// D is an ordered representation of a BSON document. This type should be used when the order of the elements matters,
// such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
//
// Example usage:
//
// 		bson.D{
   {"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
type D = primitive.D

// E represents a BSON element for a D. It is usually used inside a D.
type E = primitive.E

// M is an unordered representation of a BSON document. This type should be used when the order of the elements does not
// matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be
// serialized in an undefined, random order. If the order of the elements matters, a D should be used instead.
//
// Example usage:
//
// 		bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
type M = primitive.M

// An A is an ordered representation of a BSON array.
//
// Example usage:
//
// 		bson.A{"bar", "world", 3.14159, bson.D{
   {"qux", 12345}}}
type A = primitive.A

在 Go MongoDB Driver 中,我们调用 MongoDB Driver 提供的方法的时候,往往需要传入 bson.D, bson.A, bson.M, bson.A 这几种类型。

bson.D

用来表示一个有序的 BSON 文档,当我们需要传递一些有序的参数的时候可以使用,比如 MongoDB 中的聚合操作,聚合操作往往是一组操作的集合,比如先筛选、然后分组、然后求和,这个顺序是不能乱的。

bson.E

用来表示 bson.D 中的一个属性,类型定义如下:

// E represents a BSON element for a D. It is usually used inside a D.
type E struct {
   
	Key   string
	Value interface{
   }
}

可以类比 json 对象里面的某一个属性以及其值。

为什么不是 bson.M 中的属性呢?

首先,bson.M 其实是一个 map,不接受这种类型,bson.D 实际上是 []bson.E 类型,是 bson.E 的切片。

其次,bson.M 里面是顺序无关的,普通的 map 就已经足够了,没必要再定义一个类型。

bson.M

用来表示无需的 BSON 文档,就是一个普通的 map 类型。在保存到 MongoDB 中的时候,字段顺序是不确定的,而 bson.D 的顺序是确定的。

顺序可能大部分情况都是不需要的。不过在匹配嵌套的 BSON 数组文档的时候,可能会有问题。但还是有其他的解决办法的。

可参考本站的: MongoDB $elemMatch 操作符

bson.A

用来表示 BSON 文档中的数组类型,是有序的。

相关推荐

  1. Python变量什么类型

    2024-01-28 09:28:06       40 阅读
  2. 查看ubuntu分区什么类型

    2024-01-28 09:28:06       29 阅读
  3. arm CoreLink 什么

    2024-01-28 09:28:06       33 阅读
  4. TypeScript什么类型接口?

    2024-01-28 09:28:06       38 阅读
  5. SQL字符串类型char和varchar之间区别什么

    2024-01-28 09:28:06       25 阅读
  6. 什么PHP动态类型

    2024-01-28 09:28:06       65 阅读
  7. Go MongoDB Driver A D M E 类型什么

    2024-01-28 09:28:06       47 阅读
  8. C#面:.NET所有类型基类什么

    2024-01-28 09:28:06       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-28 09:28:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-28 09:28:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-28 09:28:06       82 阅读
  4. Python语言-面向对象

    2024-01-28 09:28:06       91 阅读

热门阅读

  1. 8-Docker网络命令之prune

    2024-01-28 09:28:06       51 阅读
  2. DAY31:贪心算法入门455、53、376

    2024-01-28 09:28:06       60 阅读
  3. MySQL中一条更新语句是怎么执行的?

    2024-01-28 09:28:06       59 阅读
  4. 代码随想录算法训练59 | 单调栈part02

    2024-01-28 09:28:06       56 阅读
  5. xss跨站脚本攻击

    2024-01-28 09:28:06       56 阅读