• MongoDB文档(二)


    Documents

    MongoDB 将数据记录存储为 BSON 文档。BSON 是JSON文档的二进制表示。BSON是一种二进制序列化格式,用于在 MongoDB 中存储文档和进行远程过程调用。

    BSON 常用类型
    TypeAlias
    Double“double”
    String“string”
    Array“array”
    ObjectId“objectId”
    Boolean“bool”
    Date“date”
    32-bit integer“int”
    64-bit integer“long”
    Timestamp“timestamp”

    文档结构

    MongoDB 文档由字段和值对组成,具有以下结构:

    {
       field1: value1,
       field2: value2,
       field3: value3,
       ...
       fieldN: valueN
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    字段的值可以是任何 BSON数据类型,包括其他文档、数组和文档数组。例如,以下文档包含不同类型的值

    {
    	_id: ObjectId("5099803df3f4948bd2f98391"),
        name: { first: "Alan", last: "Turing" },
        birth: new Date('Jun 23, 1912'),
        death: new Date('Jun 07, 1954'),
        contribs: [ "Turing machine", "Turing test", "Turingery" ],
        views : NumberLong(1250000)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    上述字段具有以下数据类型:

    _id: ObjectId。

    name: 嵌入式文档 first、last

    birth :Date类型。

    contribs :字符串数组

    views :NumberLong类型

    点符号

    MongoDB 使用点符号来访问数组中的元素和嵌入式文档的字段。

    数组

    如下数组:

    {
       ...
       contribs: [ "Turing machine", "Turing test", "Turingery" ],
       ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    要获取数组contribs中的第三个元素,需要将数组名称与点 ( .) 和索引位置连接起来,并用引号引起来:即 “contribs.2”。

    嵌入式文档

    如下文档:

    {
       ...
       name: { first: "Alan", last: "Turing" },
       contact: { phone: { type: "cell", number: "111-222-3333" } },
       ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    要获取字段name 中last,请使用点符号并且用双引号引起来"name.last"。

    文档大小限制

    BSON 文档大小不要超过16M。如果要存储大文档可以使用MongoDB 提供的 GridFS API

    文档字段顺序

    与 JavaScript 对象不同,BSON 文档中的字段是有序的。
    比较文档时,字段顺序很重要。例如,在将文档中字段a和b查询进行比较时:
    {a: 1, b: 1}等于{a: 1, b: 1}
    {a: 1, b: 1}不等于{b: 1, a: 1}

  • 相关阅读:
    Blender修改视野范围
    使用poi操作excel详解
    PaddleSeg分割框架解读[01] readme解读
    数据中心不能“偏科”,AIGC时代算力、存力需协调发展
    windows DOM 命令手册
    UE 调整材质UV贴图长宽比例
    React xlsx(工具库) 处理表头合并
    齿轮故障诊断的实验数据集及python处理
    [AHK V2]SQLite测试用例
    详解linux内核链表list_head及其接口应用
  • 原文地址:https://blog.csdn.net/nmsoftklb/article/details/127941849