
一、基本检索
1.1 条件查询
// ===== 等值 =====db.users.find({ status: "active" })// ===== 比较 =====db.orders.find({ amount: { $gt: 100 } }) // >db.orders.find({ amount: { $gte: 100 } }) // >=db.orders.find({ amount: { $lt: 500 } }) // <db.orders.find({ amount: { $gte: 100, $lte: 500 } }) // 100-500// ===== IN / NOT IN =====db.products.find({ category: { $in: ["电子", "家居"] } })db.products.find({ status: { $nin: ["废弃", "下架"] } })// ===== 不等于 =====db.users.find({ level: { $ne: "guest" } })// ===== 存在/不存在 =====db.users.find({ email: { $exists: true } }) // 有 email 字段db.users.find({ phone: { $exists: false } }) // 没有 phone 字段// ===== 类型判断 =====db.orders.find({ amount: { $type: "double" } }) // 金额为 double 类型db.orders.find({ amount: { $type: ["int", "long", "double"] } })1.2 逻辑组合
// AND (隐式, 逗号分隔)db.users.find({ status: "active", age: { $gte: 18 } })// ORdb.users.find({ $or: [ { status: "active" }, { vipLevel: { $gte: 3 } }]})// NOR (全部不满足)db.users.find({ $nor: [ { status: "banned" }, { deleted: true }]})// NOTdb.users.find({ age: { $not: { $gte: 18 } } }) // 未满 18// 组合db.orders.find({ status: "completed", $or: [{ channel: "app" }, { channel: "web" }], amount: { $gt: 100 }})1.3 正则与模糊搜索
// 以 "张" 开头db.users.find({ name: /^张/ })// 包含 "科技"db.users.find({ company: /科技/ })// 不区分大小写db.products.find({ name: /iphone/i })// 以 .com 结尾db.users.find({ email: /.com$/ })// $regex 写法db.users.find({ name: { $regex: "^张", $options: "i" } })1.4 嵌套文档与数组查询
// ===== 嵌套文档精确匹配 =====db.orders.find({ "address.city": "北京" })// ===== 数组包含 =====db.products.find({ tags: "热销" }) // tags 数组包含 "热销"db.products.find({ tags: { $all: ["热销", "新品"] } }) // 同时包含// ===== 数组大小 =====db.users.find({ hobbies: { $size: 3 } }) // 正好 3 个爱好// ===== $elemMatch: 数组元素复合条件 =====db.orders.find({ items: { $elemMatch: { product: "SKU001", qty: { $gt: 5 } } }})// ===== 数组第 N 个元素 =====db.orders.find({ "items.0.product": "SKU001" }) // 第一个元素1.5 投影与分页
// 只返回指定字段db.users.find({}, { name: 1, email: 1, _id: 0 })// 排除某些字段db.users.find({}, { password: 0, salt: 0 })// 分页 (skip + limit)db.orders.find({}).skip(20).limit(10) // 第 3 页// 游标分页 (推荐, 利用索引)db.orders.find({ _id: { $gt: lastId } }).limit(50).sort({ _id: 1 })// 排序db.orders.find({}).sort({ createdAt: -1 }) // 最新在前db.orders.find({}).sort({ status: 1, amount: -1 }) // 多字段二、更新操作
2.1 字段更新
// 设置字段db.users.updateOne( { _id: userId }, { $set: { lastLogin: new Date(), status: "active" } })// 批量更新db.orders.updateMany( { status: "expired" }, { $set: { status: "cancelled", cancelledAt: new Date() } })// 删除字段db.users.updateOne({ _id: userId }, { $unset: { tempToken: "" } })// 重命名字段db.products.updateMany({}, { $rename: { "oldName": "newName" } })// 字段不存在时设置 (insert 时生效)db.users.updateOne( { _id: userId }, { $setOnInsert: { createdAt: new Date() } }, { upsert: true })2.2 数值增减
// 原子加减db.products.updateOne({ _id: pid }, { $inc: { stock: -1, sold: 1 } })db.users.updateOne({ _id: uid }, { $inc: { points: 10 } })// 乘法db.orders.updateMany({}, { $mul: { amount: 1.13 } }) // 加 13% 税// 取最大/最小db.scores.updateOne({ _id: uid }, { $max: { highScore: 95 } })db.sensors.updateOne({ _id: sid }, { $min: { minTemp: -5 } })2.3 数组操作
// 推到数组末尾db.users.updateOne({ _id: uid }, { $push: { tags: "VIP" } })// 推到数组末尾 (去重)db.users.updateOne({ _id: uid }, { $addToSet: { tags: "VIP" } })// 一次加多个db.users.updateOne({ _id: uid }, { $push: { tags: { $each: ["新客", "首单"], $slice: -20 } } // 只保留最后 20})// 从数组移除db.users.updateOne({ _id: uid }, { $pull: { tags: "VIP" } })db.users.updateOne({ _id: uid }, { $pull: { tags: { $in: ["过期", "禁用"] } } })// 移除数组首/尾db.queue.updateOne({ _id: qid }, { $pop: { tasks: -1 } }) // 移除第一个db.queue.updateOne({ _id: qid }, { $pop: { tasks: 1 } }) // 移除最后一个// 更新数组中匹配的元素db.orders.updateOne( { _id: oid, "items.sku": "SKU001" }, { $set: { "items.$.status": "shipped" } })2.4 upsert(存在则更新,不存在则插入)
db.carts.updateOne( { userId: uid }, { $setOnInsert: { userId: uid, createdAt: new Date() }, $push: { items: { sku: "SKU001", qty: 1 } } }, { upsert: true })三、聚合查询
3.1 分组统计
// 按字段分组计数db.orders.aggregate([ { $group: { _id: "$status", count: { $sum: 1 } } }])// 按多字段分组db.orders.aggregate([ { $group: { _id: { channel: "$channel", status: "$status" }, count: { $sum: 1 }, totalAmount: { $sum: "$amount" } }}, { $sort: { totalAmount: -1 } }])// 按时间粒度分组 (天)db.orders.aggregate([ { $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } }, revenue: { $sum: "$amount" }, orders: { $sum: 1 } }}, { $sort: { _id: -1 } }, { $limit: 30 }])// 按时间粒度分组 (月)db.orders.aggregate([ { $group: { _id: { year: { $year: "$createdAt" }, month: { $month: "$createdAt" } }, revenue: { $sum: "$amount" } }}, { $sort: { "_id.year": 1, "_id.month": 1 } }])3.2 聚合运算符
db.sales.aggregate([ { $match: { date: { $gte: ISODate("2026-01-01") } } }, { $group: { _id: "$productId", total: { $sum: "$revenue" }, // 求和 avgPrice: { $avg: "$price" }, // 平均 minPrice: { $min: "$price" }, // 最小值 maxPrice: { $max: "$price" }, // 最大值 cnt: { $sum: 1 }, // 计数 firstSale:{ $first: "$date" }, // 第一个 lastSale: { $last: "$date" }, // 最后一个 products: { $addToSet: "$sku" }, // 去重集合 allPrices: { $push: "$price" } // 不去重数组 }}])3.3 过滤 → 展开 → 分组流水线
// 典型三步: 过滤 → 展开数组 → 分组统计db.articles.aggregate([ // 1. 过滤: 最近一年 { $match: { publishDate: { $gte: ISODate("2025-01-01") } } }, // 2. 展开标签 { $unwind: "$tags" }, // 3. 统计 { $group: { _id: "$tags", count: { $sum: 1 } } }, { $sort: { count: -1 } }, { $limit: 20 }])3.4 分桶统计
// 手动分桶db.users.aggregate([ { $bucket: { groupBy: "$age", boundaries: [0, 18, 25, 35, 50, 100], default: "未知", output: { count: { $sum: 1 }, avgBalance: { $avg: "$balance" } } }}])// 自动分桶db.users.aggregate([ { $bucketAuto: { groupBy: "$balance", buckets: 5, output: { count: { $sum: 1 } } }}])3.5 多维度统计(单次查询)
db.orders.aggregate([ { $match: { createdAt: { $gte: ISODate("2026-01-01") } } }, { $facet: { // 按状态 byStatus: [ { $group: { _id: "$status", count: { $sum: 1 } } } ], // 按渠道 byChannel: [ { $group: { _id: "$channel", count: { $sum: 1 } } } ], // Top 10 客户 topCustomers: [ { $group: { _id: "$userId", total: { $sum: "$amount" } } }, { $sort: { total: -1 } }, { $limit: 10 } ] }}])3.6 连表查询
// 基础连表 (orders → users)db.orders.aggregate([ { $match: { status: "completed" } }, { $lookup: { from: "users", localField: "userId", foreignField: "_id", pipeline: [ { $project: { name: 1, tier: 1, _id: 0 } } ], as: "user" }}, { $unwind: "$user" }])// 多表连查db.orders.aggregate([ { $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "user" } }, { $lookup: { from: "products", localField: "productId", foreignField: "_id", as: "product" } }, { $unwind: "$user" }, { $unwind: "$product" }, { $project: { _id: 1, amount: 1, userName: "$user.name", productName: "$product.name" }}])3.7 去重取值
// 查询某字段有哪些取值db.orders.distinct("status")// 带过滤条件的去重db.orders.distinct("channel", { createdAt: { $gte: ISODate("2026-01-01") } })// 聚合方式去重db.orders.aggregate([ { $group: { _id: "$channel" } }])3.8 结果输出
// 输出为新集合db.orders.aggregate([ { $group: { _id: "$productId", total: { $sum: "$amount" } } }, { $out: "product_summary" } // 完全替换])// 合并到已有集合db.orders.aggregate([ { $group: { _id: "$userId", daily: { $sum: "$amount" } } }, { $merge: { into: "user_stats", on: "_id", whenMatched: "merge", // 合并 whenNotMatched: "insert" // 新文档插入 }}])四、文本搜索
// 创建文本索引db.articles.createIndex({ title: "text", content: "text" })// 搜索单个词db.articles.find({ $text: { $search: "MongoDB" } })// 搜索多个词 (OR 逻辑)db.articles.find({ $text: { $search: "MongoDB 聚合" } })// 精确短语 (双引号)db.articles.find({ $text: { $search: ""聚合管线"" } })// 排除词 (-)db.articles.find({ $text: { $search: "MongoDB -MySQL" } })// 按相关性排序db.articles.find( { $text: { $search: "MongoDB" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })五、统计分析
// ===== 集合统计 =====db.orders.countDocuments() // 文档总数db.orders.countDocuments({ status: "completed" }) // 带条件db.orders.estimatedDocumentCount() // 估算 (基于元数据,快)// ===== 聚合统计 =====db.orders.aggregate([ { $match: { status: "completed" } }, { $group: { _id: null, totalRevenue: { $sum: "$amount" }, avgOrder: { $avg: "$amount" }, maxOrder: { $max: "$amount" }, orderCount: { $sum: 1 } }}])// ===== 按小时/星期分布 =====db.orders.aggregate([ { $group: { _id: { $hour: "$createdAt" }, // 小时 (0-23) // _id: { $dayOfWeek: "$createdAt" }, // 星期 (1=周日) count: { $sum: 1 } }}, { $sort: { _id: 1 } }])六、索引管理
// 创建索引db.orders.createIndex({ status: 1 }) // 单字段db.orders.createIndex({ status: 1, createdAt: -1 }) // 复合db.orders.createIndex({ userId: 1 }, { unique: true }) // 唯一db.articles.createIndex({ title: "text" }) // 文本db.locations.createIndex({ geo: "2dsphere" }) // 地理// TTL 索引 (自动过期)db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })// 查看索引db.orders.getIndexes()// 删除索引db.orders.dropIndex("status_1_createdAt_-1")db.orders.dropIndexes() // 删除所有非 _id 索引// 查看索引使用情况db.orders.aggregate([{ $indexStats: {} }])// 强制使用索引db.orders.find({ status: "completed" }).hint({ status: 1 })七、管理命令
# ===== 查看 =====show dbs # 所有数据库show collections # 当前库所有集合db.stats() # 库统计db.orders.stats() # 集合统计# ===== 复制 =====db.orders.find().forEach(function(d) { db.orders_bak.insert(d) })# ===== 删除 =====db.users.deleteOne({ _id: uid })db.users.deleteMany({ status: "banned" })db.temp.drop() # 删除集合# ===== explain =====db.orders.find({ status: "completed" }).explain("executionStats")# ===== 慢查询 =====db.setProfilingLevel(1, { slowms: 100 }) # 记录 > 100ms 的查询db.system.profile.find().sort({ ts: -1 }).limit(5).pretty() 您可能感兴趣的文章:- MongoDB分析查询性能的步骤和代码示例
- MongoDB使用索引优化查询的方法
- MongoDB分组查询、聚合查询实例
- MongoDB 正则表达式查询之如何在 MongoDB 中实现模糊搜索与索引优化陷阱
- MongoDB查询文档的各种技巧和最佳实践
- MongoDB查询时区问题示例详解
- MongoDB分页查询缓慢怎么办