MongoDB Quick Start Practice


Introduction

Nowadays, more and more semi-structured/unstructured data are generated in scenarios such as mobile applications, IoT devices, and content management systems. The fixed table structure of relational-database sometimes limits development efficiency. As a representative of document-based NoSQL, MongoDB is famous for its flexible BSON storage, dynamic schema, and powerful query index, making it very suitable for this type of scenario.

This article will help you quickly master MongoDB's quick installation, core CRUD, common indexes, and Python integration. It can be directly used in small projects. The full text is limited to 3,000 words, and there are no complex formulas.


1. What is MongoDB? (simplified version)

MongoDB is an open source non-relational-database with the following core features:

  • Document Storage: Uses JSON-like BSON format to store data, supports nested documents and arrays, and is closer to the data structure during development
  • Dynamic Schema: No need to create tables, define field types and constraints in advance, data can be flexibly adjusted as business changes
  • Production-level features: built-in index optimization, aggregation pipeline, supports replica sets (high availability), sharding (horizontal expansion of massive data)

2. Quickly install MongoDB (Docker version)

For local development, it is recommended to use Docker deployment, which can be started with one click without configuring a complex environment.

2.1 Prepare Docker Compose

Create adocker-compose.ymlfile with the following content:

version: '3.8'
services:
  mongodb:
    image: mongo:7.0
    container_name: mongodb_local
    restart: always
    environment:
      # 初始化root账号密码(本地开发可以简单,生产环境务必改)
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: 123456
    ports:
      - "27017:27017"
    volumes:
      # 数据持久化,容器删除后数据不会丢
      - mongodb_data:/data/db

volumes:
  mongodb_data:

2.2 Startup and Verification

# 后台启动MongoDB
docker-compose up -d

# 查看是否启动成功
docker ps | grep mongodb_local

# 进入容器的mongosh命令行(验证账号密码)
docker exec -it mongodb_local mongosh -u admin -p 123456

3. Literacy of core concepts (analogy and relationship type)

Afraid of not remembering? Use the analogy relational-database table to quickly understand:

MongoDB conceptAnalog relational typeDescription
DatabaseDatabaseDatabase
CollectionTableDocument collection, No need to build table structure in advance
DocumentRowSingle data record, BSON format
FieldColumnFields in the document
_idPrimary KeyAutomatically generated unique primary key (ObjectId type)

4. Basic operations (mongosh command)

4.1 Library and collection operations

// 1. 创建/切换数据库(首次插入数据才会真正创建)
use ecommerce_db

// 2. 查看所有数据库
show dbs

// 3. 查看当前数据库
db.getName()

// 4. 查看所有集合
show collections

// 5. 删除数据库(谨慎操作!)
// db.dropDatabase()

4.2 Document CRUD (emphasis!)

Insert document

// 插入单个文档(自动生成_id,自动建库建集合users)
db.users.insertOne({
  name: "张三",
  email: "zhangsan@example.com",
  age: 25,
  hobbies: ["读书", "游泳"],
  address: { city: "北京", district: "朝阳区" },
  createdAt: new Date()
})

// 批量插入(性能更高)
db.users.insertMany([
  { name: "李四", email: "lisi@example.com", age: 30, hobbies: ["摄影"] },
  { name: "王五", email: "wangwu@example.com", age: 28, hobbies: ["音乐", "登山"] }
])

Query documents

// 1. 查询单个文档
db.users.findOne({ email: "zhangsan@example.com" })

// 2. 条件查询
db.users.find({ age: { $gt: 25 } })  // 年龄>25
db.users.find({ hobbies: "读书" })  // 爱好包含"读书"
db.users.find({ name: /^张/ })  // 名字以"张"开头(模糊查询)

// 3. 投影(只返回指定字段,1显示,0隐藏,_id默认显示)
db.users.find({}, { name: 1, email: 1, _id: 0 })

// 4. 排序、分页、限制
db.users.find().sort({ age: 1 })  // 1升序,-1降序
db.users.find().skip(0).limit(10)  // 第1页,每页10条

// 5. 统计数量
db.users.countDocuments({ age: { $gt: 25 } })

Update documentation

// 1. 更新单个文档($set只修改指定字段,不覆盖整个文档)
db.users.updateOne(
  { email: "zhangsan@example.com" },
  { $set: { age: 26, updatedAt: new Date() } }
)

// 2. upsert操作(不存在则插入,存在则更新)
db.users.updateOne(
  { email: "newuser@example.com" },
  { $set: { name: "新用户", age: 20 } },
  { upsert: true }
)

Delete document

// 1. 删除单个文档
db.users.deleteOne({ email: "newuser@example.com" })

// 2. 删除多个文档(带条件,谨慎用{}删除所有!)
db.users.deleteMany({ age: { $lt: 20 } })

5. Commonly used indexes (entry level)

Indexes can greatly improve query speed, but not more is better—each index will increase the cost of writing. Here are only the 5 most commonly used ones:

// 1. 单字段索引(最基础)
db.users.createIndex({ name: 1 })

// 2. 复合索引(按「等值条件→排序→范围条件」的顺序设计效果最好)
db.users.createIndex({ "address.city": 1, age: 1 })  // 先查北京,再查年龄

// 3. 唯一索引(确保字段值不重复,比如邮箱、手机号)
db.users.createIndex({ email: 1 }, { unique: true })

// 4. TTL索引(自动删除过期文档,比如临时session、验证码)
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })  // 1小时过期

// 5. 文本索引(全文搜索,比如文章标题+内容)
db.articles.createIndex({ title: "text", content: "text" })

// 查看集合的所有索引
db.users.getIndexes()

6. Python integration (pymongo synchronized version)

6.1 Install dependencies

pip install pymongo

6.2 Basic connection and CRUD

from pymongo import MongoClient
from bson import ObjectId
from datetime import datetime

# 1. 连接MongoDB
client = MongoClient(
    "mongodb://admin:123456@localhost:27017/",
    serverSelectionTimeoutMS=5000  # 5秒超时
)

# 2. 获取数据库和集合
db = client.ecommerce_db
users_collection = db.users

# 3. 插入单个文档
def insert_user(user_data):
    user_data["createdAt"] = datetime.now()
    result = users_collection.insert_one(user_data)
    return str(result.inserted_id)

# 4. 通过邮箱查询用户
def find_user_by_email(email):
    user = users_collection.find_one({"email": email})
    # 把ObjectId转成字符串,方便前端使用
    if user:
        user["_id"] = str(user["_id"])
    return user

# 5. 分页查询用户
def find_users_paginated(page=1, limit=10):
    skip = (page - 1) * limit
    users = list(
        users_collection.find({}, {"password": 0})  # 隐藏敏感字段
        .skip(skip)
        .limit(limit)
        .sort("createdAt", -1)
    )
    # 批量转ObjectId
    for user in users:
        user["_id"] = str(user["_id"])
    return users

# 测试
if __name__ == "__main__":
    user_id = insert_user({
        "name": "测试用户",
        "email": "test@example.com",
        "age": 22
    })
    print(f"插入的用户ID:{user_id}")
    print(find_user_by_email("test@example.com"))

7. Entry-Level Best Practices

  1. Document structure: Prioritize using nested documents (such as user addresses), and do not nest them too deeply (generally no more than 3 levels); only use references for frequently changing shared data (such as product classification).
  2. Index design: Equivalence condition fields come first, range conditions come last; use them regularlydb.users.aggregate([{$indexStats: {}}])Check the index usage and delete the useless ones.
  3. Query Optimization: Always use projection to limit the fields returned (hiding sensitive fields can also reduce data transmission); try to avoid using$where(Full table scan will be performed).
  4. Security configuration: Do not use the root account to connect to the application, create a user with specific library read and write permissions; do not expose port 27017 to the public network in the production environment.

Summarize

This article takes you through a quick start with MongoDB: from Docker deployment to core CRUD, from common indexes to Python integration. If you want to learn more, you can read the official documentation and study replica sets, sharding, more complex aggregation pipelines, multi-document transactions, etc.