Mongodb常见操作符和运算符

MongoDB 提供了丰富的操作符(Operators)和运算符(Expressions)用于在查询和更新文档时指定条件和操作数据。

查询操作符(Query Operators)

$eq - 等于

javascript 复制代码
db.collection.find({ field: { $eq: value } })
// 示例:查找所有名字等于 "Tom" 的文档
db.users.find({ name: { $eq: "Tom" } })

$gt - 大于

javascript 复制代码
db.collection.find({ field: { $gt: value } })
// 示例:查找所有年龄大于 25 的用户
db.users.find({ age: { $gt: 25 } })

$gte - 大于等于

javascript 复制代码
db.collection.find({ field: { $gte: value } })
// 示例:查找所有年龄大于等于 25 的用户
db.users.find({ age: { $gte: 25 } })

$lt - 小于

javascript 复制代码
db.collection.find({ field: { $lt: value } })
// 示例:查找所有年龄小于 25 的用户
db.users.find({ age: { $lt: 25 } })

$lte - 小于等于

javascript 复制代码
db.collection.find({ field: { $lte: value } })
// 示例:查找所有年龄小于等于 25 的用户
db.users.find({ age: { $lte: 25 } })

$ne - 不等于

javascript 复制代码
db.collection.find({ field: { $ne: value } })
// 示例:查找所有名字不是 "Tom" 的文档
db.users.find({ name: { $ne: "Tom" } })

$in - 在数组中

javascript 复制代码
db.collection.find({ field: { $in: array } })
// 示例:查找兴趣包含 "阅读" 或 "游泳" 的用户
db.users.find({ interests: { $in: ["阅读", "游泳"] } })

$nin - 不在数组中

javascript 复制代码
db.collection.find({ field: { $nin: array } })
// 示例:查找兴趣不含 "阅读" 和 "游泳" 的用户
db.users.find({ interests: { $nin: ["阅读", "游泳"] } })

$or - 或者

javascript 复制代码
db.collection.find({ $or: [{ condition1 }, { condition2 }] })
// 示例:查找名字为 "Tom" 或年龄大于 25 的用户
db.users.find({ $or: [{ name: "Tom" }, { age: { $gt: 25 } }] })

$and - 并且

javascript 复制代码
db.collection.find({ $and: [{ condition1 }, { condition2 }] })
// 示例:查找名字为 "Tom" 并且年龄大于 25 的用户
db.users.find({ $and: [{ name: "Tom" }, { age: { $gt: 25 } }] })

$not - 非

javascript 复制代码
db.collection.find({ field: { $not: { operator: value } } })
// 示例:查找年龄不小于 25 的用户
db.users.find({ age: { $not: { $lt: 25 } } })

$exists - 字段存在

javascript 复制代码
db.collection.find({ field: { $exists: true or false } })
// 示例:查找有 `email` 字段的用户
db.users.find({ email: { $exists: true } })

更新操作符(Update Operators)

$set - 设置字段的值

javascript 复制代码
db.collection.update({ query }, { $set: { field: value } })
// 示例:更新名字为 "Tom" 的用户的年龄为 30
db.users.update({ name: "Tom" }, { $set: { age: 30 } })

$unset - 删除字段

javascript 复制代码
db.collection.update({ query }, { $unset: { field: "" } })
// 示例:删除名字为 "Tom" 的用户的 `age` 字段
db.users.update({ name: "Tom" }, { $unset: { age: "" } })

$inc - 增加数值字段的值

javascript 复制代码
db.collection.update({ query }, { $inc: { field: value } })
// 示例:将名字为 "Tom" 的用户的年龄增加 2
db.users.update({ name: "Tom" }, { $inc: { age: 2 } })

$push - 向数组字段添加元素

javascript 复制代码
db.collection.update({ query }, { $push: { field: value } })
// 示例:向名字为 "Tom" 的用户的兴趣列表添加 "足球"
db.users.update({ name: "Tom" }, { $push: { interests: "足球" } })

$pull - 从数组字段删除元素

javascript 复制代码
db.collection.update({ query }, { $pull: { field: value } })
// 示例:从名字为 "Tom" 的用户的兴趣列表中删除 "足球"
db.users.update({ name: "Tom" }, { $pull: { interests: "足球" } })

$addToSet - 向数组添加元素,但不创建重复项

javascript 复制代码
db.collection.update({ query }, { $addToSet: { field: value } })
// 示例:向名字为 "Tom" 的用户的兴趣列表中添加 "游泳",如果 "游泳" 已存在,则忽略
db.users.update({ name: "Tom" }, { $addToSet: { interests: "游泳" } })

$each - 与 $push$addToSet 配合,向数组添加多个元素

javascript 复制代码
db.collection.update({ query }, { $push: { field: { $each: [value1, value2] } } })
// 示例:一次性向名字为 "Tom" 的用户的兴趣列表中添加多个兴趣
db.users.update({ name: "Tom" }, { $push: { interests: { $each: ["绘画", "舞蹈"] } } })

$pop - 从数组的开头或末尾删除元素

javascript 复制代码
// $pop: 1 从末尾移除,$pop: -1 从开头移除
db.collection.update({ query }, { $pop: { field: 1 or -1 } })
// 示例:从名字为 "Tom" 的用户的兴趣列表末尾删除一个兴趣
db.users.update({ name: "Tom" }, { $pop: { interests: 1 } })

聚合管道操作符(Aggregation Pipeline Operators)

$match - 过滤数据

javascript 复制代码
db.collection.aggregate([ { $match: { field: value } } ])
// 示例:筛选所有年龄大于 25 的用户
db.users.aggregate([ { $match: { age: { $gt: 25 } } }])

$group - 按字段分组

javascript 复制代码
db.collection.aggregate([
  { $group: { _id: "$field", count: { $sum: 1 } } }
])
// 示例:按兴趣分组计数用户
db.users.aggregate([
  { $group: { _id: "$interests", count: { $sum: 1 } } }
])

$project - 指定输出字段

javascript 复制代码
db.collection.aggregate([ { $project: { field1: 1, field2: 0 } } ])
// 示例:输出用户的名字和兴趣,不输出其他字段
db.users.aggregate([ { $project: { name: 1, interests: 1 } }])

$sort - 排序

javascript 复制代码
db.collection.aggregate([ { $sort: { field: 1 or -1 } } ])
// 1 代表升序, -1 代表降序
// 示例:按年龄升序排列用户
db.users.aggregate([ { $sort: { age: 1 } }])

以上是MongoDB中的一些常用操作符和运算符,它们可以帮你构建灵活和强大的数据操作指令。在实际的应用中,会将多个操作符组合起来使用,以满足复杂的业务逻辑。


English version

Detailed Introduction to MongoDB Operators and Expressions

MongoDB offers a rich set of operators (Operators) and expressions (Expressions) for specifying conditions and manipulating data when querying and updating documents.

Query Operators

$eq - Equal

javascript 复制代码
db.collection.find({ field: { $eq: value } })
// Example: Find all documents where the name is equal to "Tom"
db.users.find({ name: { $eq: "Tom" } })

$gt - Greater Than

javascript 复制代码
db.collection.find({ field: { $gt: value } })
// Example: Find all users older than 25
db.users.find({ age: { $gt: 25 } })

$gte - Greater Than or Equal To

javascript 复制代码
db.collection.find({ field: { $gte: value } })
// Example: Find all users aged 25 or older
db.users.find({ age: { $gte: 25 } })

$lt - Less Than

javascript 复制代码
db.collection.find({ field: { $lt: value } })
// Example: Find all users younger than 25
db.users.find({ age: { $lt: 25 } })

$lte - Less Than or Equal To

javascript 复制代码
db.collection.find({ field: { $lte: value } })
// Example: Find all users aged 25 or younger
db.users.find({ age: { $lte: 25 } })

$ne - Not Equal

javascript 复制代码
db.collection.find({ field: { $ne: value } })
// Example: Find all documents where the name is not "Tom"
db.users.find({ name: { $ne: "Tom" } })

$in - In an Array

javascript 复制代码
db.collection.find({ field: { $in: array } })
// Example: Find users whose interests include "Reading" or "Swimming"
db.users.find({ interests: { $in: ["Reading", "Swimming

"] } })

$nin - Not in an Array

javascript 复制代码
db.collection.find({ field: { $nin: array } })
// Example: Find users whose interests do not include "Reading" and "Swimming"
db.users.find({ interests: { $nin: ["Reading", "Swimming"] } })

$or - Or

javascript 复制代码
db.collection.find({ $or: [{ condition1 }, { condition2 }] })
// Example: Find users who are either named "Tom" or are older than 25
db.users.find({ $or: [{ name: "Tom" }, { age: { $gt: 25 } }] })

$and - And

javascript 复制代码
db.collection.find({ $and: [{ condition1 }, { condition2 }] })
// Example: Find users who are named "Tom" and are older than 25
db.users.find({ $and: [{ name: "Tom" }, { age: { $gt: 25 } }] })

$not - Not

javascript 复制代码
db.collection.find({ field: { $not: { operator: value } } })
// Example: Find users who are not younger than 25
db.users.find({ age: { $not: { $lt: 25 } } })

$exists - Field Exists

javascript 复制代码
db.collection.find({ field: { $exists: true or false } })
// Example: Find users who have an `email` field
db.users.find({ email: { $exists: true } })

Update Operators

$set - Set the Value of a Field

javascript 复制代码
db.collection.update({ query }, { $set: { field: value } })
// Example: Update the age of users named "Tom" to 30
db.users.update({ name: "Tom" }, { $set: { age: 30 } })

$unset - Remove a Field

javascript 复制代码
db.collection.update({ query }, { $unset: { field: "" } })
// Example: Remove the `age` field from users named "Tom"
db.users.update({ name: "Tom" }, { $unset: { age: "" } })

$inc - Increment a Numeric Field's Value

javascript 复制代码
db.collection.update({ query }, { $inc: { field: value } })
// Example: Increment the age of users named "Tom" by 2
db.users.update({ name: "Tom" }, { $inc: { age: 2 } })

$push - Add an Element to an Array Field

javascript 复制代码
db.collection.update({ query }, { $push: { field: value } })
// Example: Add "Football" to the interests list of users named "Tom"
db.users.update({ name: "Tom" }, { $push: { interests: "Football" } })

$pull - Remove an Element from an Array Field

javascript 复制代码
db.collection.update({ query }, { $pull: { field: value } })
// Example: Remove "Football" from the interests list of users named "Tom"
db.users.update({ name: "Tom" }, { $pull: { interests: "Football" } })

$addToSet - Add an Element to an Array without Creating Duplicates

javascript 复制代码
db.collection.update({ query }, { $addToSet: { field: value } })
// Example: Add "Swimming" to the interests list of users named "Tom", if "Swimming" isn't already

present
db.users.update({ name: "Tom" }, { $addToSet: { interests: "Swimming" } })

$each - Combined with $push or $addToSet, Add Multiple Elements to an Array

javascript 复制代码
db.collection.update({ query }, { $push: { field: { $each: [value1, value2] } } })
// Example: Add multiple interests at once to the interests list of users named "Tom"
db.users.update({ name: "Tom" }, { $push: { interests: { $each: ["Painting", "Dancing"] } } })

$pop - Remove an Element from the Beginning or End of an Array

javascript 复制代码
// $pop: 1 removes from the end, $pop: -1 removes from the beginning
db.collection.update({ query }, { $pop: { field: 1 or -1 } })
// Example: Remove an interest from the end of the interests list of users named "Tom"
db.users.update({ name: "Tom" }, { $pop: { interests: 1 } })

Aggregation Pipeline Operators

$match - Filter Data

javascript 复制代码
db.collection.aggregate([ { $match: { field: value } } ])
// Example: Filter all users older than 25
db.users.aggregate([ { $match: { age: { $gt: 25 } } }])

$group - Group by Field

javascript 复制代码
db.collection.aggregate([
  { $group: { _id: "$field", count: { $sum: 1 } } }
])
// Example: Group users by interests and count them
db.users.aggregate([
  { $group: { _id: "$interests", count: { $sum: 1 } } }
])

$project - Specify Output Fields

javascript 复制代码
db.collection.aggregate([ { $project: { field1: 1, field2: 0 } } ])
// Example: Output user names and interests, excluding other fields
db.users.aggregate([ { $project: { name: 1, interests: 1 } }])

$sort - Sort

javascript 复制代码
db.collection.aggregate([ { $sort: { field: 1 or -1 } } ])
// 1 for ascending order, -1 for descending
相关推荐
爱电摇的小码农21 分钟前
【深度探究系列(5)】:前端开发打怪升级指南:从踩坑到封神的解决方案手册
前端·javascript·css·vue.js·node.js·html5·xss
lihainuo3 小时前
Next.js + AI-SDK 实战:模型注册表从类型设计到工具调用全解析
后端·node.js
胡gh4 小时前
JavaScript 中的闭包、防抖与节流:让你彻底搞懂它们的作用和应用场景
前端·javascript·node.js
野槐4 小时前
vue3+node.js+mysql写接口(二)
node.js
讨厌吃蛋黄酥4 小时前
🚀 全栈开发48小时逆袭:用Node.js打造超炫实时数据仪表盘! 📊
node.js·全栈
天若有情6735 小时前
Node.js 是什么?npm 是什么? Vue 为什么需要他们?
vue.js·npm·node.js
爱敲代码的小冰6 小时前
npm 切换 node 版本 和npm的源
前端·npm·node.js
甜瓜看代码15 小时前
1.
react.js·node.js·angular.js
伍哥的传说15 小时前
React 实现五子棋人机对战小游戏
前端·javascript·react.js·前端框架·node.js·ecmascript·js
01传说17 小时前
vue3 配置安装 pnpm 报错 已解决
java·前端·vue.js·前端框架·npm·node.js