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

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: 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 文档中的数组类型,是有序的。


相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
2月前
|
JSON 安全 前端开发
类型安全的 Go HTTP 请求
类型安全的 Go HTTP 请求
|
7天前
|
存储 Go
Go: struct 结构体类型和指针【学习笔记记录】
本文是Go语言中struct结构体类型和指针的学习笔记,包括结构体的定义、成员访问、使用匿名字段,以及指针变量的声明使用、指针数组定义使用和函数传参修改值的方法。
|
2月前
|
存储 NoSQL Go
Go 使用 MongoDB
Go 使用 MongoDB
28 8
|
2月前
|
人工智能 NoSQL Go
Go MongoDB Driver 实例
Go MongoDB Driver 实例
15 1
|
2月前
|
安全 Go
|
2月前
|
存储 安全 程序员
|
2月前
|
编译器 Go 开发者
Go 在编译时评估隐式类型的优点详解
【8月更文挑战第31天】
24 0
|
2月前
|
存储 编译器 Go
|
2月前
|
存储 安全 Go
深入理解 Go 语言中的指针类型
【8月更文挑战第31天】
13 0
|
9天前
|
存储 NoSQL 关系型数据库
非关系型数据库-MongoDB技术(二)
非关系型数据库-MongoDB技术(二)
下一篇
无影云桌面