【Mongo DB】万字详解,Mongo DB的简介到实战使用(下)

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: 今天主要为大家提供一条龙服务,从Mongo DB的简介到实战使用,使我们面对技术选型的时候可以得心应手。

索引

MongoDB 的索引是基于 B-tree 数据结构及对应算法形成的。_id默认会创建索引

网络异常,图片无法展示
|

索引类型

单列索引

除了 MongoDB 定义的 _id 索引之外,MongoDB 还支持在文档的单个字段上创建用户定义的升序/降序索引。

网络异常,图片无法展示
|

>db.collection.createlndex ( { source: 1 } )  //1 为升序,-1 为降序

复合索引

MongoDB 也支持复合索引,并且复合索引的规则和MySQL基本一致。复合索引中列出的字段顺序具有重要意义。例如,如果复合索引由 { userid: 1, score: -1 } 组成,则索引首先按userid正序排序,然后在每个userid的值内,再在按score倒序排序。

网络异常,图片无法展示
|

>db.collection.createIndex ({ "score": -1, "userid": 1 })

多键索引

若要为包含数组的字段建立索引,MongoDB 会为数组中的每个元素创建索引键。这些多键值索引支持对数组字段的高效查询

网络异常,图片无法展示
|

索引新增删除

  • 创建索引
db.collection.createIndex( { name: -1 } )
db.user.getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  { v: 2, key: { name: 1 }, name: 'name_1' }
]
Atlas atlas-119lob-shard-0 [primary] test> db.user.createIndex({name: 1},{name:"idx_name"})
idx_name
Atlas atlas-119lob-shard-0 [primary] test> db.user.getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  { v: 2, key: { name: 1 }, name: 'idx_name' }
]
  • 删除索引
db.user.dropIndexes("name_1")

执行计划

explain有三种模式,分别是:queryPlanner、executionStats、allPlansExecution。

  • queryPlanner:输出索引的候选索引,包括最优索引及其执行stage过程(winningPlan)+其他非最优候选索引及其执行stage过程
  • executionStats:相比queryPlanner参数,executionStats会记录查询优化器根据所选最优索引执行SQL的整个过程信息,会真正执行整个SQL。
  • allPlansExecution:和executionStats类似,只是多了所有候选索引的执行过程

新增10000条测试数据

for (let i = 1; i <= 500000; i++) db.item.insertOne({_id:i, item_id: "10010_" + i, item_order: "P1HNHN" + i, test:"test_"+i+"TES"})
Atlas atlas-119lob-shard-0 [primary] order> db.item.find({item_id:"10010_38976"}).explain("executionStats")
{
  explainVersion: '1',
  queryPlanner: {
    namespace: 'order.item',
    indexFilterSet: false,
    parsedQuery: { item_id: { '$eq': '10010_38976' } },
    maxIndexedOrSolutionsReached: false,
    maxIndexedAndSolutionsReached: false,
    maxScansToExplodeReached: false,
    winningPlan: {
      stage: 'COLLSCAN',
      filter: { item_id: { '$eq': '10010_38976' } },
      direction: 'forward'
    },
    rejectedPlans: []
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 1,
    executionTimeMillis: 23,
    totalKeysExamined: 0,
    totalDocsExamined: 50000,
    executionStages: {
      stage: 'COLLSCAN',
      filter: { item_id: { '$eq': '10010_38976' } },
      nReturned: 1,
      executionTimeMillisEstimate: 2,
      works: 50002,
      advanced: 1,
      needTime: 50000,
      needYield: 0,
      saveState: 50,
      restoreState: 50,
      isEOF: 1,
      direction: 'forward',
      docsExamined: 50000
    }
  },
  command: { find: 'item', filter: { item_id: '10010_38976' }, '$db': 'order' },
  serverInfo: {
    host: 'cluster0-shard-00-02.fdv5c.mongodb.net',
    port: 27017,
    version: '5.0.8',
    gitVersion: 'c87e1c23421bf79614baf500fda6622bd90f674e'
  },
  serverParameters: {
    internalQueryFacetBufferSizeBytes: 104857600,
    internalQueryFacetMaxOutputDocSizeBytes: 104857600,
    internalLookupStageIntermediateDocumentMaxSizeBytes: 16793600,
    internalDocumentSourceGroupMaxMemoryBytes: 104857600,
    internalQueryMaxBlockingSortMemoryUsageBytes: 33554432,
    internalQueryProhibitBlockingMergeOnMongoS: 0,
    internalQueryMaxAddToSetBytes: 104857600,
    internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
  },
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp(1, 1651821606),
    signature: {
      hash: Binary(Buffer.from("9250e56618dba0aacb504c846045c8e534adb68f", "hex"), 0),
      keyId: Long("7066952799589761026")
    }
  },
  operationTime: Timestamp(1, 1651821606)
}

在item_id上创建索引

db.item.createIndex ({ "item_id": 1},{"name":"idx_item_id" })
Atlas atlas-119lob-shard-0 [primary] order> db.item.find({item_id:"10010_38976"}).explain("executionStats")
{
  explainVersion: '1',
  queryPlanner: {
    namespace: 'order.item',
    indexFilterSet: false,
    parsedQuery: { item_id: { '$eq': '10010_38976' } },
    maxIndexedOrSolutionsReached: false,
    maxIndexedAndSolutionsReached: false,
    maxScansToExplodeReached: false,
    winningPlan: {
      stage: 'FETCH',
      inputStage: {
        stage: 'IXSCAN',
        keyPattern: { item_id: 1 },
        indexName: 'idx_item_id',
        isMultiKey: false,
        multiKeyPaths: { item_id: [] },
        isUnique: false,
        isSparse: false,
        isPartial: false,
        indexVersion: 2,
        direction: 'forward',
        indexBounds: { item_id: [ '["10010_38976", "10010_38976"]' ] }
      }
    },
    rejectedPlans: []
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 1,
    executionTimeMillis: 0,
    totalKeysExamined: 1,
    totalDocsExamined: 1,
    executionStages: {
      stage: 'FETCH',
      nReturned: 1,
      executionTimeMillisEstimate: 0,
      works: 2,
      advanced: 1,
      needTime: 0,
      needYield: 0,
      saveState: 0,
      restoreState: 0,
      isEOF: 1,
      docsExamined: 1,
      alreadyHasObj: 0,
      inputStage: {
        stage: 'IXSCAN',
        nReturned: 1,
        executionTimeMillisEstimate: 0,
        works: 2,
        advanced: 1,
        needTime: 0,
        needYield: 0,
        saveState: 0,
        restoreState: 0,
        isEOF: 1,
        keyPattern: { item_id: 1 },
        indexName: 'idx_item_id',
        isMultiKey: false,
        multiKeyPaths: { item_id: [] },
        isUnique: false,
        isSparse: false,
        isPartial: false,
        indexVersion: 2,
        direction: 'forward',
        indexBounds: { item_id: [ '["10010_38976", "10010_38976"]' ] },
        keysExamined: 1,
        seeks: 1,
        dupsTested: 0,
        dupsDropped: 0
      }
    }
  },
  command: { find: 'item', filter: { item_id: '10010_38976' }, '$db': 'order' },
  serverInfo: {
    host: 'cluster0-shard-00-02.fdv5c.mongodb.net',
    port: 27017,
    version: '5.0.8',
    gitVersion: 'c87e1c23421bf79614baf500fda6622bd90f674e'
  },
  serverParameters: {
    internalQueryFacetBufferSizeBytes: 104857600,
    internalQueryFacetMaxOutputDocSizeBytes: 104857600,
    internalLookupStageIntermediateDocumentMaxSizeBytes: 16793600,
    internalDocumentSourceGroupMaxMemoryBytes: 104857600,
    internalQueryMaxBlockingSortMemoryUsageBytes: 33554432,
    internalQueryProhibitBlockingMergeOnMongoS: 0,
    internalQueryMaxAddToSetBytes: 104857600,
    internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
  },
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp(2, 1651821695),
    signature: {
      hash: Binary(Buffer.from("035c24b6cc83e5d1d5bca97befb43a2c2fc30605", "hex"), 0),
      keyId: Long("7066952799589761026")
    }
  },
  operationTime: Timestamp(2, 1651821695)
}

事务

什么是writeConcern?

writeConcern 决定一个写操作落到多少个节点上才算成功。writeConcern 的取值包括:

  • 0:发起写操作,不关心是否成功;
  • 1~集群最大数据节点数:写操作需要被复制到指定节点数才算成功;
  • majority:写操作需要被复制到大多数节点上才算成功。

发起写操作的程序将阻塞到写操作到达指定的节点数为止

网络异常,图片无法展示
|

majority

网络异常,图片无法展示
|

副本集

MongoDB的副本集主要用于实现服务的高可用。

  • 数据写入时将数据迅速复制到其他节点上
  • 在Primary节点出现故障后自动选举一个Secondary节点作为Primary

网络异常,图片无法展示
|

Golang操作MongoDB

var client *mongo.Client
type Item struct {
  ItemId string `bson:"item_id"`
  ItemOrder string `bson:"item_order"`
  Test string `bson:"test"`
  UpdateTime string `bson:"update_time"`
}
func main() {
  initDB()
  collection := client.Database("order").Collection("item")
  result, err := collection.InsertOne(context.TODO(), &Item{
    ItemId:     "test_item_id",
    ItemOrder:  "test_item_order",
    Test:       "test_test",
    UpdateTime: time.Now().Format("2006-01-02 15:04:05"),
  })
  if err != nil{
    log.Fatal(err)
  }
  fmt.Println(result.InsertedID)
  // find()
}
func find(){
  initDB()
  collection := client.Database("order").Collection("item")
  cursor, err := collection.Find(context.TODO(), bson.D{{"itemid", "test_item_id"}})
  if err != nil {
    log.Fatal(err)
  }
  defer cursor.Close(context.TODO())
  for cursor.Next(context.TODO()) {
    item := &Item{}
    err := cursor.Decode(item)
    if err != nil {
      log.Fatal(err)
    }
    fmt.Println(item)
  }
}
func initDB() {
  clientOptions := options.Client().ApplyURI(url)
  ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  defer cancel()
  c, err := mongo.Connect(ctx, clientOptions)
  if err != nil {
    log.Fatal(err)
  }
  err3 := c.Ping(ctx, nil)
  if err3 != nil {
    log.Fatal(err3)
  }
  fmt.Println("mongodb connected")
  client = c
}

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
9月前
|
NoSQL Java 测试技术
|
9月前
|
NoSQL Java 测试技术
spring boot MongoDB实战(二)
spring boot MongoDB实战
167 1
|
9月前
|
NoSQL Java MongoDB
Spring Boot中MongoDB的使用和实战
Spring Boot中MongoDB的使用和实战
207 0
|
6月前
|
存储 NoSQL JavaScript
MongoDB存储过程实战:聚合框架、脚本、最佳实践,一文全掌握!
【8月更文挑战第24天】MongoDB是一款备受欢迎的文档型NoSQL数据库,以灵活的数据模型和强大功能著称。尽管其存储过程支持不如传统关系型数据库,本文深入探讨了MongoDB在此方面的最佳实践。包括利用聚合框架处理复杂业务逻辑、封装业务逻辑提高复用性、运用JavaScript脚本实现类似存储过程的功能以及考虑集成其他工具提升数据处理能力。通过示例代码展示如何创建订单处理集合并定义验证规则,虽未直接实现存储过程,但有效地演示了如何借助JavaScript脚本处理业务逻辑,为开发者提供更多实用指导。
113 2
|
6月前
|
NoSQL Java 测试技术
5-MongoDB实战演练
本文档详细介绍了如何使用MongoDB实现头条文章的评论系统。主要功能包括基本的增删改查API、根据文章ID查询评论、以及评论的点赞功能。文章分析了表结构设计,明确了各字段的意义,并给出了具体的字段类型。技术选型方面,文档推荐使用mongodb-driver作为Java连接MongoDB的驱动包,同时介绍了Spring Data MongoDB这一更高层次的持久层框架。此外,文档还提供了搭建文章微服务模块的具体步骤,包括项目工程的搭建、实体类的编写、索引的添加方式等,并展示了如何使用MongoTemplate实现评论点赞功能。
|
8月前
|
存储 NoSQL MongoDB
MongoDB实战面试指南:常见问题一网打尽
MongoDB实战面试指南:常见问题一网打尽
|
8月前
|
NoSQL Java 关系型数据库
非关系型数据库NoSQL数据层解决方案 之 Mongodb 简介 下载安装 springboot整合与读写操作
非关系型数据库NoSQL数据层解决方案 之 Mongodb 简介 下载安装 springboot整合与读写操作
95 0
|
9月前
|
存储 NoSQL 关系型数据库
MongoDB非关系型数据库实战
【5月更文挑战第6天】MongoDB,流行的NoSQL数据库,以其灵活的数据模型和高性能备受青睐。本文介绍了MongoDB的基础,包括文档型数据库特性、安装配置、数据操作。通过电商订单管理的实战案例,展示了MongoDB在处理复杂数据结构和大规模数据时的优势,适用于电商、游戏、视频直播等场景。MongoDB的索引、全文搜索和地理空间功能进一步增强了其实用性。注意性能优化和扩展性以确保系统稳定性和可靠性。
|
9月前
|
NoSQL Java MongoDB
Spring Boot 整合 MongoDB 实战
本文介绍了如何使用Spring Boot整合MongoDB,实现数据持久化。步骤包括:环境准备(安装Java、MongoDB及创建Spring Boot项目)、在pom.xml中添加MongoDB依赖、配置MongoDB连接信息、创建映射MongoDB集合的实体类、定义MongoDB Repository接口、编写业务逻辑和服务层以及控制器层。通过测试确保整合成功。此实战教程帮助读者理解Spring Boot与MongoDB整合的基础,适用于快速构建Java应用。
919 11
|
9月前
|
存储 NoSQL 关系型数据库
MongoDB简介以及核心概念
MongoDB简介以及核心概念
78 1