🗄️ MongoDB Overview

🗄️ MongoDB 概述

  • MongoDB: A leading NoSQL database known for flexibility, scalability, and support for unstructured data.
  • MongoDB: 一种领先的 NoSQL 数据库,以其灵活性、可伸缩性和对非结构化数据的支持而闻名。

⚙️ Administration

⚙️ 管理

  • Key Responsibilities:
  • 主要职责:
    • Installation: Set up MongoDB on cloud infrastructure.
    • 安装: 在云基础设施上设置 MongoDB。
    • Configuration: Use mongod.conf for settings (e.g., storage engines, network).
    • 配置: 使用 mongod.conf 进行设置(例如,存储引擎、网络)。
    • Management: Handle databases, collections, and indexes to optimize performance.
    • 管理: 处理数据库、集合和索引以优化性能。
  • Performance Monitoring:
  • 性能监控:
    • Tools like mongostat and MongoDB Atlas track key metrics (CPU usage, memory, query times).
    • mongostatMongoDB Atlas 等工具可跟踪关键指标(CPU 使用率、内存、查询时间)。
    • Sharding: Distributes data across multiple servers for scalability.
    • 分片: 将数据分布到多个服务器以实现可伸缩性。

🔒 Security Practices

🔒 安全实践

Security in MongoDB is crucial for protecting sensitive data.

MongoDB 中的安全对于保护敏感数据至关重要。

  • Authentication Methods:
  • 身份验证方法:
    • SCRAM, LDAP, x.509 certificates.
    • SCRAMLDAPx.509 证书
  • Role-Based Access Control (RBAC):
  • 基于角色的访问控制 (RBAC):
    • Manage permissions for different user roles (developers, admins, analysts).
    • 管理不同用户角色(开发人员、管理员、分析师)的权限。
  • Data Encryption:
  • 数据加密:
    • At rest: Using WiredTiger storage engine’s encryption.
    • 静态加密: 使用 WiredTiger 存储引擎的加密功能。
    • In transit: TLS/SSL certificates to secure communications.
    • 传输中加密: 使用 TLS/SSL 证书保护通信安全。
  • Auditing: Tracks database operations to ensure compliance and identify breaches.
  • 审计: 跟踪数据库操作以确保合规性并识别违规行为。

Java Integration

Java 集成

  • MongoDB Java Driver: Facilitates CRUD operations and data management in Java applications.
  • MongoDB Java 驱动程序: 方便在 Java 应用程序中执行 CRUD 操作和数据管理。
  • Example Use Cases:
  • 用例示例:
    • CRUD Operations: Store customer profiles as JSON-like documents.
    • CRUD 操作: 将客户资料存储为类 JSON 文档。
    • Aggregation Framework: Analyze customer behavior for marketing insights.
    • 聚合框架: 分析客户行为以获取营销洞察。
    • Transactions: Ensure data consistency during high-volume sales.
    • 事务: 在大批量销售期间确保数据一致性。

🔍 Finding Slow Operations

🔍 查找慢操作

  • Use the db.currentOp() function to identify slow-running operations:
  • 使用 db.currentOp() 函数识别运行缓慢的操作:
1
db.currentOp()
  • Key Output Fields:
  • 关键输出字段:
    • opid: Unique operation identifier.
    • opid: 唯一操作标识符。
    • active: Indicates if the operation is currently running.
    • active: 指示操作当前是否正在运行。
    • secs_running: Duration of operation in seconds.
    • secs_running: 操作持续时间(秒)。
    • microsecs_running: Duration in microseconds.
    • microsecs_running: 持续时间(微秒)。

🛠️ Database Operations in MongoDB

🛠️ MongoDB 中的数据库操作

Key Commands

关键命令

  • Operation Types:
  • 操作类型:
    • Query: Retrieve data.
    • 查询: 检索数据。
    • Insert: Add data.
    • 插入: 添加数据。
    • Update: Modify existing data.
    • 更新: 修改现有数据。
    • Remove: Delete data.
    • 删除: 删除数据。

🔒 Connection Identifier

🔒 连接标识符

Client Identifier: A unique identifier for the client, useful for correlating with logs (e.g., [conn3]).

客户端标识符: 客户端的唯一标识符,可用于与日志关联(例如 [conn3])。


🔒 Lock Management

🔒 锁管理

  • Locks:

  • :

    • Types of Locks: Describes the kinds of locks taken during operations.
    • 锁的类型: 描述操作期间获取的锁的种类。
    • Waiting for Locks: Indicates if an operation is blocked while waiting for a lock.
    • 等待锁: 指示操作是否因等待锁而被阻塞。

🔄 Operation Yielding

🔄 操作让步

  • Yielding:

  • 让步:

    • Occurs when an operation releases its lock to allow other operations to proceed.
    • 当一个操作释放其锁以允许其他操作继续进行时发生。
    • Operations only yield if there are waiting operations.
    • 仅当存在等待操作时,操作才会让步。

⏱️ Lock Acquisition Time

⏱️ 锁获取时间

  • Time Acquiring Locks:

  • 获取锁的时间:

    • Measured in microseconds; can be useful for performance diagnostics.
    • 以微秒为单位测量;可用于性能诊断。

🔍 Finding Problematic Operations

🔍 查找有问题的操作

  • Use of db.currentOp():

  • db.currentOp() 的使用:

    • Filter operations to find slow ones:

    • 筛选操作以查找慢操作:

      1
      2
      3
      4
      5
      db.currentOp({
      "active": true,
      "secs_running": { "$gt": 3 },
      "ns": /^db1\./
      })

Killing Operations

终止操作

  • Terminate Unwanted Operations:

  • 终止不需要的操作:

    • Use db.killOp(opid) to kill an operation by its operation ID.
    • 使用 db.killOp(opid) 按操作 ID 终止操作。
    • Operations can only be killed if they yield.
    • 只有当操作让步时才能被终止。

📊 Using System Profiler

📊 使用系统分析器

  • Purpose: To record and analyze slow operations.

  • 目的: 记录和分析慢操作。

  • Enabling Profiler:

  • 启用分析器:

    • Activate with: db.setProfilingLevel(2) (profiles all operations).
    • 使用 db.setProfilingLevel(2) 激活(分析所有操作)。
    • Level 1 profiles operations taking longer than a specified time (default 100 ms).
    • 级别 1 分析超过指定时间(默认为 100 毫秒)的操作。
    • Turn off profiling with: db.setProfilingLevel(0).
    • 使用 db.setProfilingLevel(0) 关闭分析。

📏 Calculating Sizes

📏 计算大小

Documents and Collections Size

文档和集合大小

  • Document Size: Use Object.bsonsize(document) to get document size in bytes.

  • 文档大小: 使用 Object.bsonsize(document) 获取文档大小(以字节为单位)。

    • Example:

    • 示例:

      1
      Object.bsonsize({_id: ObjectId()}) // 22 bytes
  • Collection Stats:

  • 集合统计信息:

    • Use db.collection.stats() for overall collection information (size, count, avgObjSize, etc.).
    • 使用 db.collection.stats() 获取集合的总体信息(大小、计数、平均对象大小等)。

Example of Collection Stats:

集合统计信息示例:

Field字段 Description描述
ns Namespace of the collection
ns 集合的命名空间
size Total size of the documents (bytes)
size 文档总大小(字节)
count Number of documents
count 文档数量
avgObjSize Average object size
avgObjSize 平均对象大小
storageSize Size used by the storage engine
storageSize 存储引擎使用的大小

📊 MongoDB Collection and Database Statistics

📊 MongoDB 集合和数据库统计信息

Collection Stats

集合统计

  • Size: Total bytes used by documents when uncompressed.
  • 大小 (Size): 文档未压缩时使用的总字节数。
  • Storage Size: Space saved by compression; can be less than the size.
  • 存储大小 (Storage Size): 通过压缩节省的空间;可以小于原始大小。
  • Count: Total documents in the collection.
  • 计数 (Count): 集合中的文档总数。
  • Average Object Size (avgObjSize): Average size of documents.
  • 平均对象大小 (avgObjSize): 文档的平均大小。

Indexing

索引

  • nindexes: Number of indexes on the collection.

  • nindexes: 集合上的索引数量。

  • Indexes are typically larger than the data they store.

  • 索引通常比它们存储的数据大。

  • Right-Balanced Indexes:

  • 右平衡索引:

    • Randomly distributed indexes: ~50% free space.
    • 随机分布的索引:约 50% 的可用空间。
    • Ascending-order indexes: ~10% free space.
    • 升序索引:约 10% 的可用空间。

Scaling Factor for Stats

统计数据的缩放因子

  • Use scaling factor for large sizes:
  • 对大尺寸使用缩放因子:
    • 1024 for kilobytes
    • 1024 表示千字节
    • 1024×1024 for megabytes
    • 1024×1024 表示兆字节
    • Example: db.big.stats(1024 * 1024 * 1024 * 1024) for terabytes.
    • 示例:db.big.stats(1024 * 1024 * 1024 * 1024) 表示太字节。


🏛️ Database Stats

🏛️ 数据库统计

  • Database Name: Identifies the database.
  • 数据库名称: 标识数据库。
  • Collections: Number of collections it contains.
  • 集合: 其包含的集合数量。
  • Views: Total views in the database.
  • 视图: 数据库中的视图总数。
  • Objects: Total count of documents across collections.
  • 对象: 所有集合中的文档总数。
  • Data Size: Size of uncompressed data.
  • 数据大小: 未压缩数据的大小。
  • Storage Size: Size considering compression.
  • 存储大小: 考虑压缩后的大小。
  • Index Size: Space occupied by all indexes.
  • 索引大小: 所有索引占用的空间。

Filesystem Statistics

文件系统统计

  • fsTotalSize: Total disk capacity for MongoDB data storage.
  • fsTotalSize: MongoDB 数据存储的总磁盘容量。
  • fsUsedSize: Space currently used by MongoDB files.
  • fsUsedSize: MongoDB 文件当前使用的空间。

Example Command

示例命令

  • To get database stats:

  • 获取数据库统计信息:

    1
    db.stats()

Key Takeaways

关键要点

  • Understanding collection and database statistics is crucial for optimizing performance and managing storage effectively.
  • 理解集合和数据库统计信息对于优化性能和有效管理存储至关重要。
  • Compression impacts storage size, making it essential to monitor both size and storage size for efficient data management.
  • 压缩会影响存储大小,因此监控原始大小和存储大小对于高效的数据管理至关重要。

🛠️ MongoDB Administration and Tools

🛠️ MongoDB 管理和工具

Database Statistics

数据库统计

  • db.stats():

  • db.stats():

    • Returns zero values for nonexistent databases.
    • 对于不存在的数据库返回零值。
    • High lock percent can slow down listing databases; avoid if possible.
    • 高锁定百分比会减慢列出数据库的速度;如果可能,请避免。

MongoDB Command-Line Tools

MongoDB 命令行工具

  1. mongotop:
  2. mongotop:
    • Overview of busy collections.
    • 繁忙集合的概览。
    • Use mongotop --locks for locking statistics.
    • 使用 mongotop --locks 获取锁定统计信息。
  3. mongostat:
  4. mongostat:
    • Provides server-wide statistics at configurable intervals.
    • 以可配置的间隔提供服务器范围的统计信息。
    • Key fields reported:
    • 报告的关键字段:
      • insert/query/update/delete/getmore/command: Counts of operations.
      • insert/query/update/delete/getmore/command: 操作计数。
      • flushes: Times data flushed to disk.
      • flushes: 数据刷新到磁盘的次数。
      • mapped: Memory mapped by MongoDB, roughly equal to data directory size.
      • mapped: MongoDB 映射的内存,约等于数据目录大小。
      • vsize: Virtual memory usage, typically twice the data directory size.
      • vsize: 虚拟内存使用量,通常是数据目录大小的两倍。
      • res: Total memory used by MongoDB.
      • res: MongoDB 使用的总内存。
      • locked db: Database most locked in the last interval.
      • locked db: 上一个间隔中锁定最多的数据库。
      • idx miss %: Percentage of index accesses that resulted in page faults.
      • idx miss %: 导致页面错误的索引访问百分比。
      • queue size (qr|qw): Blocking reads and writes.
      • queue size (qr|qw): 阻塞的读写队列大小。
      • active clients (ar|aw): Current active read/write clients.
      • active clients (ar|aw): 当前活动的读/写客户端。
      • netIn/netOut: Network bytes in and out.
      • netIn/netOut: 网络输入/输出字节数。
      • conn: Open connections.
      • conn: 打开的连接数。

🔑 Security: Authentication and Authorization

🔑 安全:身份验证和授权

Authentication vs. Authorization

身份验证与授权

  • Authentication: Verifies user identity.
  • 身份验证: 验证用户身份。
  • Authorization: Determines resource access based on user roles.
  • 授权: 根据用户角色确定资源访问权限。

Authentication Mechanisms

身份验证机制

  • SCRAM: Salted Challenge Response Authentication.
  • SCRAM: 加盐质询响应身份验证机制。
  • x.509: Uses public key infrastructure for certificate verification.
  • x.509: 使用公钥基础设施进行证书验证。
  • Enterprise Support: Kerberos and LDAP proxy authentication.
  • 企业版支持: Kerberos 和 LDAP 代理身份验证。

User Roles and Privileges

用户角色和权限

  • Built-in Roles:

  • 内置角色:

    • read: Access to read data.
    • read: 读取数据权限。
    • readWrite: Read and modify data.
    • readWrite: 读取和修改数据权限。
    • dbAdmin: Administrative tasks.
    • dbAdmin: 管理任务权限。
    • userAdmin: Manage users and roles.
    • userAdmin: 管理用户和角色权限。
    • dbOwner: Combines readWrite, dbAdmin, and userAdmin roles.
    • dbOwner: 结合了 readWrite、dbAdmin 和 userAdmin 角色权限。
    • clusterManager: Manage and monitor the cluster.
    • clusterManager: 管理和监控集群权限。
    • root: Full access across all operations and resources.
    • root: 对所有操作和资源的完全访问权限。

Creating Users

创建用户

  • A user is created in a specific database known as the authentication database.
  • 用户在称为身份验证数据库的特定数据库中创建。
  • Privileges can extend beyond the authentication database to other resources.
  • 权限可以扩展到身份验证数据库之外的其他资源。

🔐 Using x.509 Certificates for Authentication

🔐 使用 x.509 证书进行身份验证

  • Trust Hierarchy: Requires a trusted certification authority (CA) to sign all certificates.
  • 信任层次结构: 需要受信任的证书颁发机构 (CA) 来签署所有证书。
  • Member Certificates: Used for internal authentication within a cluster.
  • 成员证书: 用于集群内部的身份验证。
  • Client Authentication: Clients must authenticate with the primary and secondaries.
  • 客户端身份验证: 客户端必须向主节点和从节点进行身份验证。

Important Note

重要提示

  • Authentication and authorization are not enabled by default; must be explicitly configured using the --auth option or the MongoDB config file.
  • 默认情况下未启用身份验证和授权;必须使用 --auth 选项或 MongoDB 配置文件显式配置。

🔐 Certificate Structure

🔐 证书结构

  • A certificate for authentication has a structured format including:
  • 用于身份验证的证书具有结构化格式,包括:
    • Version
    • 版本
    • Serial Number
    • 序列号
    • Signature Algorithm
    • 签名算法
    • Issuer
    • 颁发者
    • Validity (Not Before, Not After)
    • 有效期 (生效日期, 失效日期)
    • Subject
    • 使用者
    • Subject Public Key Info
    • 使用者公钥信息

🗝️ Key Properties for x.509 Certificates in MongoDB

🗝️ MongoDB 中 x.509 证书的关键属性

A member certificate must have:

成员证书必须具有:

  • All certificates issued by a single Certificate Authority (CA).
  • 所有证书均由单个证书颁发机构 (CA) 颁发。
  • Distinguished Name (DN) must specify at least one of:
  • 辨别名 (DN) 必须至少指定以下之一:
    • Organization (O)
    • 组织 (O)
    • Organizational Unit (OU)
    • 组织单位 (OU)
    • Domain Component (DC)
    • 域组件 (DC)
  • Matching attributes for all cluster members.
  • 所有集群成员的属性匹配。

🔧 Establishing a CA

🔧 建立 CA

  • Generating a Root CA:

  • 生成根 CA:

    • Use OpenSSL to create a key pair and configuration file.

    • 使用 OpenSSL 创建密钥对和配置文件。

    • Example command to generate a key:

    • 生成密钥的示例命令:

      1
      openssl genrsa -out root-ca.key 4096
  • Create a self-signed certificate using:

  • 使用以下命令创建自签名证书:

    1
    openssl req -new -x509 -days 1826 -key root-ca.key -out root-ca.crt -config openssl.cnf -subj "$dn_prefix/CN=ROOTCA"


📜 OpenSSL Configuration File Sections

📜 OpenSSL 配置文件部分

  1. policy_match: Defines matching policies for certificates.
  2. policy_match: 定义证书的匹配策略。
  3. req: Settings for certificate requests.
  4. req: 证书请求的设置。
  5. v3_req: Extensions for certificate requests.
  6. v3_req: 证书请求的扩展。
  7. v3_ca: Extensions for CA certificates.
  8. v3_ca: CA 证书的扩展。


🔍 Certificate Verification

🔍 证书验证

  • To check the contents of the generated root certificate:

  • 检查生成的根证书的内容:

    Bash

    1
    openssl x509 -noout -text -in root-ca.crt
  • The output includes:

  • 输出包括:

    • Issuer
    • 颁发者
    • Validity period
    • 有效期
    • Subject Public Key Info (algorithm and key size)
    • 使用者公钥信息 (算法和密钥大小)


🏗️ Key Concepts

🏗️ 关键概念

  • Root CA: The ultimate source of trust; self-signed for isolated networks or testing.
  • 根 CA: 最终的信任来源;用于隔离网络或测试的自签名证书。
  • OpenSSL: A critical tool for generating certificates and managing keys.
  • OpenSSL: 生成证书和管理密钥的关键工具。

🛡️ Creating and Managing CAs in MongoDB

🛡️ 在 MongoDB 中创建和管理 CA

Root CA and Intermediate CA

根 CA 和中间 CA

  • Root CA: The foundational certificate authority that signs other certificates.
  • 根 CA: 签署其他证书的基础证书颁发机构。
  • Intermediate CA: A certificate signed by the root CA, used for signing member and client certificates. This adds a layer of security, as revoking an intermediate CA affects only its issued certificates.
  • 中间 CA: 由根 CA 签署的证书,用于签署成员和客户端证书。这增加了一层安全性,因为撤销中间 CA 仅影响其颁发的证书。

Best Practice: Use an intermediate CA to minimize risk. If compromised, only part of the trust tree is affected.

最佳实践: 使用中间 CA 来最小化风险。如果泄露,只会影响信任树的一部分。

Steps to Create an Intermediate CA:

创建中间 CA 的步骤:

  1. Generate a signing key:

  2. 生成签名密钥:

    Bash

    1
    openssl genrsa -out signing-ca.key 4096
  3. Create a signing request and generate the certificate:

  4. 创建签名请求并生成证书:

    Bash

    1
    2
    openssl req -new -key signing-ca.key -out signing-ca.csr -config openssl.cnf -subj "$dn_prefix/CN=CA-SIGNER"
    openssl x509 -req -days 730 -in signing-ca.csr -CA root-ca.crt -CAkey root-ca.key -set_serial 01 -out signing-ca.crt -extfile openssl.cnf -extensions v3_ca
  5. Concatenate certificates into a single PEM file:

  6. 将证书连接成单个 PEM 文件:

    Bash

    1
    2
    cat root-ca.crt > root-ca.pem
    cat signing-ca.crt >> root-ca.pem


🔑 Generating and Signing Certificates

🔑 生成和签署证书

Member Certificates (x.509 Server Certificates)

成员证书 (x.509 服务器证书)

  • Used by MongoDB members to authenticate within the cluster.
  • MongoDB 成员用于在集群内进行身份验证。

Steps to Generate Member Certificates:

生成成员证书的步骤:

Bash

1
2
3
4
5
6
7
for host in "${mongodb_server_hosts[@]}"; do
openssl genrsa -out ${host}.key 4096
openssl req -new -key ${host}.key -out ${host}.csr -config openssl.cnf -subj "$dn_prefix/OU=$ou_member/CN=${host}"
openssl x509 -req -days 365 -in ${host}.csr -CA signing-ca.crt -CAkey signing-ca.key -CAcreateserial -out ${host}.crt -extfile openssl.cnf -extensions v3_req
cat ${host}.crt > ${host}.pem
cat ${host}.key >> ${host}.pem
done

Client Certificates

客户端证书

  • Used by MongoDB tools (mongo shell, Compass, etc.) and applications.
  • MongoDB 工具(mongo shell、Compass 等)和应用程序使用。

Steps to Generate Client Certificates:

生成客户端证书的步骤:

Bash

1
2
3
4
5
6
7
for host in "${mongodb_client_hosts[@]}"; do
openssl genrsa -out ${host}.key 4096
openssl req -new -key ${host}.key -out ${host}.csr -config openssl.cnf -subj "$dn_prefix/OU=$ou_client/CN=${host}"
openssl x509 -req -days 365 -in ${host}.csr -CA signing-ca.crt -CAkey signing-ca.key -CAcreateserial -out ${host}.crt -extfile openssl.cnf -extensions v3_req
cat ${host}.crt > ${host}.pem
cat ${host}.key >> ${host}.pem
done


🚀 Starting Replica Set Without Authentication

🚀 在没有身份验证的情况下启动副本集

Procedure:

步骤:

  • Start each member of the replica set without authentication:
  • 在没有身份验证的情况下启动副本集的每个成员:

Bash

1
2
3
4
5
for host in "${mongodb_server_hosts[@]}"; do
mkdir -p ./db/${host}
mongod --replSet set509 --port $mport --dbpath ./db/$host --fork --logpath ./db/${host}.log
let "mport++"
done

Initialize the Replica Set:

初始化副本集:

  • Use a JavaScript file to initiate the replica set:
  • 使用 JavaScript 文件初始化副本集:

Bash

1
2
3
4
5
6
7
8
9
10
myhostname=`hostname`
cat > init_set.js <<EOF
rs.initiate();
mport=$mongodb_port;
mport++;
rs.add("localhost:" + mport);
mport++;
rs.add("localhost:" + mport);
EOF
mongo localhost:$mongodb_port init_set.js


🌐 Java Integration with MongoDB

🌐 Java 与 MongoDB 集成

  • Java: A leading programming language, popular for its versatility across industries. Its object-oriented nature and “write once, run anywhere” capability make it highly desirable.
  • Java: 一种领先的编程语言,因其在各行各业的多功能性而广受欢迎。其面向对象的特性和“一次编写,到处运行”的能力使其非常受欢迎。
  • MongoDB: The top NoSQL database, known for its document-oriented design, which supports building scalable applications efficiently.
  • MongoDB: 顶级的 NoSQL 数据库,以其面向文档的设计而闻名,该设计支持高效构建可扩展的应用程序。


This guide summarizes the process of creating and managing certificate authorities (CAs) for MongoDB, generating and signing member and client certificates, starting a replica set, and integrating Java with MongoDB.

本指南总结了为 MongoDB 创建和管理证书颁发机构 (CA)、生成和签署成员及客户端证书、启动副本集以及将 Java 与 MongoDB 集成的过程。

📦 MongoDB Atlas Setup

📦 MongoDB Atlas 设置

  • MongoDB Atlas:

  • MongoDB Atlas

    :

    • Fully-managed, cloud-native database service
    • 完全托管的云原生数据库服务
    • Features: full-text search, charts, mobile synchronization, GraphQL support
    • 功能:全文搜索、图表、移动同步、GraphQL 支持


⚙️ Project Setup

⚙️ 项目设置

  • Maven Project Creation:

  • Maven 项目创建:

    • Option 1: Clone the git repository

    • 选项 1:克隆 git 仓库

      • Command: git clone git@github.com:mongodb−developer/java−quick−start.git
      • 命令:git clone git@github.com:mongodb−developer/java−quick−start.git
    • Option 2: Create manually

    • 选项 2:手动创建

      • Folder structure:

      • 文件夹结构:

        1
        2
        3
        4
        5
        6
        7
        8
        java-quick-start/
        ├── pom.xml
        └── src
        └── main
        └── java
        └── com
        └── mongodb
        └── quickstart
  • pom.xml Configuration:

  • pom.xml 配置:

    • Define project properties and dependencies:

    • 定义项目属性和依赖项:

      XML

      1
      2
      3
      4
      5
      <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <mongodb-driver-sync.version>5.0.0</mongodb-driver-sync.version>
      <logback-classic.version>1.2.13</logback-classic.version>
      </properties>


💻 Creating a Hello World Program

💻 创建 Hello World 程序

  • HelloMongoDB.java:

    1
    2
    3
    4
    5
    6
    package com.mongodb.quickstart;
    public class HelloMongoDB {
    public static void main(String[] args) {
    System.out.println("Hello MongoDB!");
    }
    }
  • Run Command:

  • 运行命令:

    • Using Maven:

    • 使用 Maven:

      1
      mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.HelloMongoDB"


🌐 Connecting with Java

🌐 使用 Java 连接

  • MongoClient Initialization:

  • MongoClient 初始化:

    • Use the connection string:

    • 使用连接字符串:

      1
      String connectionString = System.getProperty("mongodb.uri");
  • Pre-flight Check:

  • 飞行前检查:

    • Command: ping: 1

    • 命令:ping: 1

    • Check using:

    • 使用以下方式检查:

      1
      2
      3
      4
      5
      static boolean preFlightChecks(MongoClient mongoClient) {
      Document pingCommand = new Document("ping", 1);
      Document response = mongoClient.getDatabase("admin").runCommand(pingCommand);
      return response.get("ok", Number.class).intValue() == 1;
      }
  • Display Databases:

  • 显示数据库:

    • Print using:

    • 使用以下命令打印:

      1
      2
      List<Document> databases = mongoClient.listDatabases().into(new ArrayList<>());
      databases.forEach(db -> System.out.println(db.toJson()));


📊 Sample Dataset Structure

📊 示例数据集结构

  • Collection: grades (in database: sample_training)

  • 集合:grades (在数据库 sample_training 中)

  • Document Example:

  • 文档示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    {
    "_id": ObjectId("56d5f7eb604eb380b0d8d8ce"),
    "student_id": 0,
    "scores": [
    {"type": "exam", "score": 78.4},
    {"type": "quiz", "score": 73.4},
    {"type": "homework", "score": 46.9},
    {"type": "homework", "score": 76.7}
    ],
    "class_id": 339
    }
  • BSON vs. Extended JSON:

  • BSON 与扩展 JSON:

    • BSON stores key-value pairs with types, allowing MongoDB to understand data types explicitly.
  • BSON 存储带有类型的键值对,允许 MongoDB 明确理解数据类型。



📝 Next Steps

📝 后续步骤

  • Set up a free MongoDB Atlas cluster if not done already.
  • 如果尚未完成,请设置一个免费的 MongoDB Atlas 集群。
  • Continue with creating additional classes for data operations (like Create, Read, Update, Delete).
  • 继续创建用于数据操作(如创建、读取、更新、删除)的其他类。

🗃️ Java and MongoDB: Inserting Documents

🗃️ Java 和 MongoDB:插入文档

🛠️ Setting Up the MongoDB Connection

🛠️ 设置 MongoDB 连接

  • Establish Connection:

  • 建立连接:

    1
    2
    3
    MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"));
    MongoDatabase sampleTrainingDB = mongoClient.getDatabase("sample_training");
    MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("grades");

📝 Creating a BSON Document

📝 创建 BSON 文档

BSON Document is a binary representation of JSON-like documents.

BSON 文档 是类 JSON 文档的二进制表示形式。

  • Document Structure:

  • 文档结构:

    • Use Document class to represent a student.
    • 使用 Document 类表示学生。
    • Include fields such as _id, student_id, class_id, and scores.
    • 包括 _idstudent_idclass_idscores 等字段。
  • Example Code:

  • 示例代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    Document student = new Document("_id", new ObjectId())
    .append("student_id", 10000d)
    .append("class_id", 1d)
    .append("scores", List.of(
    new Document("type", "exam").append("score", rand.nextDouble() * 100),
    new Document("type", "quiz").append("score", rand.nextDouble() * 100),
    new Document("type", "homework").append("score", rand.nextDouble() * 100),
    new Document("type", "homework").append("score", rand.nextDouble() * 100)
    ));

🚀 Inserting a Document

🚀 插入文档

  • Single Document Insertion:

  • 单个文档插入:

    1
    gradesCollection.insertOne(student);

🔄 Inserting Multiple Documents

🔄 插入多个文档

  • Batch Insertion:

  • 批量插入:

    • Instead of inserting one by one, wrap documents in a list and use insertMany.
    • 不要逐个插入,而是将文档包装在列表中并使用 insertMany
  • Example Code for Multiple Inserts:

  • 多个插入的示例代码:

    1
    2
    3
    4
    5
    List<Document> grades = new ArrayList<>();
    for (double classId = 1d; classId <= 10d; classId++) {
    grades.add(generateNewGrade(10001d, classId));
    }
    gradesCollection.insertMany(grades, new InsertManyOptions().ordered(false));

📦 Grade Generation Method

📦 成绩生成方法

  • Factory Method to create grade documents:

  • 工厂方法创建成绩文档:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    private static Document generateNewGrade(double studentId, double classId) {
    List<Document> scores = List.of(
    new Document("type", "exam").append("score", rand.nextDouble() * 100),
    new Document("type", "quiz").append("score", rand.nextDouble() * 100),
    new Document("type", "homework").append("score", rand.nextDouble() * 100),
    new Document("type", "homework").append("score", rand.nextDouble() * 100)
    );
    return new Document("_id", new ObjectId())
    .append("student_id", studentId)
    .append("class_id", classId)
    .append("scores", scores);
    }

⚠️ Handling Errors with Insertions

⚠️ 处理插入错误

  • Ordered vs. Unordered Inserts:

  • 有序插入与无序插入:

    • Ordered: Stops on the first error.
    • 有序: 遇到第一个错误即停止。
    • Unordered: Continues processing and reports errors after all attempts.
    • 无序: 继续处理并在所有尝试后报告错误。

📜 Final Code Structure

📜 最终代码结构

  • The complete Java class should include methods for both single and multiple document insertions, as well as the grade generation method.
  • 完整的 Java 类应包括单个和多个文档插入的方法,以及成绩生成方法。


📚 MongoDB Operations in Java

📚 Java 中的 MongoDB 操作

📝 Creating and Reading Documents

📝 创建和读取文档

  • Create Class: Create

  • 创建类: Create

    • Creates documents in the grades collection for students with IDs 10000 and 10001.
    • grades 集合中为 ID 为 1000010001 的学生创建文档。
  • Read Class: Read

  • 读取类: Read

    • Use the find method to retrieve documents.

    • 使用 find 方法检索文档。

    • Example command:

    • 示例命令:

      1
      Document student1 = gradesCollection.find(new Document("student_id", 10000)).first();
    • Output format:

    • 输出格式:

      1
      2
      3
      4
      5
      6
      {
      "_id": ObjectId("5daa0e274f52b44cfea94652"),
      "student_id": 10000,
      "class_id": 1,
      "scores": [ ... ]
      }

🔍 Understanding BSON and Types

🔍 理解 BSON 和类型

BSON (Binary JSON) is a binary-encoded serialization format for JSON-like documents.

BSON (二进制 JSON) 是一种用于类 JSON 文档的二进制编码序列化格式。

  • Type Handling:

  • 类型处理:

    • student_id and class_id are treated as doubles.
    • student_idclass_id 被视为双精度浮点数。
    • MongoDB performs type conversion during comparisons.
    • MongoDB 在比较期间执行类型转换。

🛠️ Updating Documents

🛠️ 更新文档

  • Update Class: Update

  • 更新类: Update

    • Use updateOne to modify a document.

    • 使用 updateOne 修改文档。

    • Example:

    • 示例:

      1
      2
      3
      Bson filter = eq("student_id", 10000);
      Bson updateOperation = set("comment", "You should learn MongoDB!");
      UpdateResult updateResult = gradesCollection.updateOne(filter, updateOperation);
  • Output:

  • 输出:

    • Confirms the document was updated, showing the modified document.
    • 确认文档已更新,显示修改后的文档。

🔄 Upserting Documents

🔄 更新插入文档 (Upsert)

  • Upsert Operation: Combines insert and update.

  • 更新插入操作: 结合了插入和更新。

    • Example:

    • 示例:

      1
      UpdateOptions options = new UpdateOptions().upsert(true);
    • Used when the document may not exist, such as adding a comment to a non-existent student.

    • 当文档可能不存在时使用,例如向不存在的学生添加评论。

🔄 Updating Multiple Documents

🔄 更新多个文档

  • Use updateMany to apply updates to multiple documents.

  • 使用 updateMany 将更新应用于多个文档。

    • Example:

    • 示例:

      1
      2
      filter = eq("student_id", 10001);
      updateResult = gradesCollection.updateMany(filter, updateOperation);

🔄 findOneAndUpdate Method

🔄 findOneAndUpdate 方法

  • Purpose: Combines finding and updating a document in one operation.

  • 目的: 在一个操作中结合查找和更新文档。

  • Example:

  • 示例:

    1
    Document oldVersion = gradesCollection.findOneAndUpdate(filter, updates);
  • Multiple updates can be combined using combine.

  • 可以使用 combine 组合多个更新。

🔑 Key Operations Summary

🔑 关键操作摘要

Operation操作 Method方法 Description描述
Create Document insertOne Inserts a new document into a collection.
创建文档 insertOne 将新文档插入到集合中。
Read Document find Retrieves documents based on a filter.
读取文档 find 根据筛选器检索文档。
Update Document updateOne Updates a single document matching the filter.
更新文档 updateOne 更新与筛选器匹配的单个文档。
Upsert Document updateOne with upsert Creates a document if it doesn’t exist.
更新插入文档 upsertupdateOne 如果文档不存在则创建文档。
Update Many Documents updateMany Updates all documents matching the filter.
更新多个文档 updateMany 更新所有与筛选器匹配的文档。
Find and Update findOneAndUpdate Combines finding and updating a document.
查找并更新 findOneAndUpdate 结合查找和更新文档。

This study guide covers the basic CRUD operations in MongoDB using Java, along with handling BSON types and the significance of upsert operations.

本学习指南涵盖了使用 Java 在 MongoDB 中进行基本 CRUD 操作,以及处理 BSON 类型和更新插入操作的重要性。

🛠️ MongoDB Update Operations

🛠️ MongoDB 更新操作

Update Document Syntax

更新文档语法

  • To update a specific document, use:

  • 更新特定文档,请使用:

    1
    2
    3
    Bson filter = eq("student_id", 10000);
    Bson updateOperation = set("comment", "You should learn MongoDB!");
    UpdateResult updateResult = gradesCollection.updateOne(filter, updateOperation);

FindOneAndUpdate

FindOneAndUpdate

FindOneAndUpdate retrieves and updates a document atomically.

FindOneAndUpdate 原子地检索和更新文档。

  • Example:

  • 示例:

    1
    Document oldVersion = gradesCollection.findOneAndUpdate(filter, updates);
  • To get the new version after the update, use:

  • 要在更新后获取新版本,请使用:

    1
    FindOneAndUpdateOptions optionAfter = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);

Update Operators

更新操作符

  • set: Assigns a specified value to a field.
  • set: 为字段分配指定的值。
  • inc: Increments a field’s value.
  • inc: 增加字段的值。
  • rename: Changes a field’s name.
  • rename: 更改字段的名称。
  • mul: Multiplies a field’s value by a specified number.
  • mul: 将字段的值乘以指定的数字。
  • addToSet: Adds a value to an array only if it does not already exist.
  • addToSet: 仅当值不存在时才将其添加到数组中。

Update Many Documents

更新多个文档

  • To apply updates to multiple documents:

  • 将更新应用于多个文档:

    1
    updateResult = gradesCollection.updateMany(filter, updateOperation);

🗑️ MongoDB Delete Operations

🗑️ MongoDB 删除操作

Delete One Document

删除单个文档

  • To delete a single document:

  • 删除单个文档:

    1
    2
    Bson filter = eq("student_id", 10000);
    DeleteResult result = gradesCollection.deleteOne(filter);

FindOneAndDelete

FindOneAndDelete

FindOneAndDelete retrieves and deletes a document in one operation.

FindOneAndDelete 在一个操作中检索并删除文档。

  • Example:

  • 示例:

    1
    Document doc = gradesCollection.findOneAndDelete(filter);

Delete Many Documents

删除多个文档

  • To delete multiple documents based on a condition:

  • 根据条件删除多个文档:

    1
    2
    Bson filter = gte("student_id", 10000);
    DeleteResult result = gradesCollection.deleteMany(filter);

Drop Collection

删除集合

  • To remove an entire collection and its metadata:

  • 删除整个集合及其元数据:

    1
    gradesCollection.drop();


📜 Code Structure for Updates and Deletes

📜 更新和删除的代码结构

Update Example Code

更新示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mongodb.quickstart;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.bson.conversions.Bson;

public class Update {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
MongoDatabase sampleTrainingDB = mongoClient.getDatabase("sample_training");
MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("grades");
// Update operations here
// 此处执行更新操作
}
}
}

Delete Example Code

删除示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mongodb.quickstart;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import org.bson.Document;
import org.bson.conversions.Bson;

public class Delete {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
MongoDatabase sampleTrainingDB = mongoClient.getDatabase("sample_training");
MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("grades");
// Delete operations here
// 此处执行删除操作
}
}
}

🛠️ MongoDB Administration & Security

🛠️ MongoDB 管理与安全

Key Features of MongoDB

MongoDB 的主要特性

  • NoSQL Database: Known for its flexibility, scalability, and ability to manage unstructured data effectively.
  • NoSQL 数据库: 以其灵活性、可伸缩性以及有效管理非结构化数据的能力而闻名。
  • Storage Engines: WiredTiger is recommended for efficient data compression and performance optimization.
  • 存储引擎: 推荐使用 WiredTiger 以实现高效的数据压缩和性能优化。


⚙️ Administration Tasks

⚙️ 管理任务

  • Installation & Configuration: Essential for efficient and secure database operations.
  • 安装与配置: 高效和安全数据库操作的基础。
  • Database Management: Involves creating and managing databases, collections, and indexes.
  • 数据库管理: 包括创建和管理数据库、集合和索引。

Indexes

索引

Indexes improve query performance but require careful management to balance read and write operations.
索引可以提高查询性能,但需要仔细管理以平衡读写操作。



📊 Monitoring Tools

📊 监控工具

  • mongostat Tool:
  • mongostat 工具:
    • Purpose: Monitors key metrics like CPU usage and memory consumption.
    • 目的: 监控 CPU 使用率和内存消耗等关键指标。
    • Utility: Helps administrators maintain performance and identify issues swiftly.
    • 用途: 帮助管理员维护性能并快速识别问题。


🔐 Security in MongoDB

🔐 MongoDB 安全

  • Authentication Mechanisms:
  • 身份验证机制:
    • SCRAM
    • SCRAM
    • LDAP
    • LDAP
    • x.509 certificates
    • x.509 证书

RBAC (Role-Based Access Control): Allows specific roles and permissions to users based on their job functions.
RBAC (基于角色的访问控制): 根据用户的工作职能授予特定的角色和权限。

Data Protection

数据保护

  • Encryption:
  • 加密:
    • At Rest: Protects stored data.
    • 静态加密: 保护存储的数据。
    • In Transit: Secured using TLS/SSL certificates.
    • 传输中加密: 使用 TLS/SSL 证书进行保护。


🔄 MongoDB Features for Data Management

🔄 MongoDB 数据管理特性

  • Aggregation Framework: Enables real-time analytics by grouping and analyzing data.
  • 聚合框架: 通过对数据进行分组和分析来实现实时分析。
  • Sharding: Distributes data across multiple servers for horizontal scaling.
  • 分片: 将数据分布到多个服务器以实现水平扩展。
  • Backup Methods:
  • 备份方法:
    • mongodump
    • mongodump
    • File system snapshots
    • 文件系统快照
    • Cloud-based backups
    • 基于云的备份

📝 Summary of Important Concepts

📝 重要概念总结

  • MongoDB excels in managing unstructured data with effective administration practices.
  • MongoDB 擅长通过有效的管理实践来管理非结构化数据。
  • Security features ensure robust data protection and compliance with regulations.
  • 安全功能可确保强大的数据保护并符合法规要求。
  • Tools and frameworks aid in optimizing performance and facilitate seamless integration with applications, especially Java.
  • 工具和框架有助于优化性能,并促进与应用程序(尤其是 Java)的无缝集成。