Mongodb常见操作符和运算符
MongoDB 提供了丰富的操作符(Operators)和运算符(Expressions)用于在查询和更新文档时指定条件和操作数据。
查询操作符(Query Operators)
$eq
- 等于
db.collection.find({ field: { $eq: value } })
// 示例:查找所有名字等于 "Tom" 的文档
db.users.find({ name: { $eq: "Tom" } })
$gt
- 大于
db.collection.find({ field: { $gt: value } })
// 示例:查找所有年龄大于 25 的用户
db.users.find({ age: { $gt: 25 } })
$gte
- 大于等于
db.collection.find({ field: { $gte: value } })
// 示例:查找所有年龄大于等于 25 的用户
db.users.find({ age: { $gte: 25 } })
$lt
- 小于
db.collection.find({ field: { $lt: value } })
// 示例:查找所有年龄小于 25 的用户
db.users.find({ age: { $lt: 25 } })
$lte
- 小于等于
db.collection.find({ field: { $lte: value } })
// 示例:查找所有年龄小于等于 25 的用户
db.users.find({ age: { $lte: 25 } })
$ne
- 不等于
db.collection.find({ field: { $ne: value } })
// 示例:查找所有名字不是 "Tom" 的文档
db.users.find({ name: { $ne: "Tom" } })
$in
- 在数组中
db.collection.find({ field: { $in: array } })
// 示例:查找兴趣包含 "阅读" 或 "游泳" 的用户
db.users.find({ interests: { $in: ["阅读", "游泳"] } })
$nin
- 不在数组中
db.collection.find({ field: { $nin: array } })
// 示例:查找兴趣不含 "阅读" 和 "游泳" 的用户
db.users.find({ interests: { $nin: ["阅读", "游泳"] } })
$or
- 或者
db.collection.find({ $or: [{ condition1 }, { condition2 }] })
// 示例:查找名字为 "Tom" 或年龄大于 25 的用户
db.users.find({ $or: [{ name: "Tom" }, { age: { $gt: 25 } }] })
$and
- 并且
db.collection.find({ $and: [{ condition1 }, { condition2 }] })
// 示例:查找名字为 "Tom" 并且年龄大于 25 的用户
db.users.find({ $and: [{ name: "Tom" }, { age: { $gt: 25 } }] })
$not
- 非
db.collection.find({ field: { $not: { operator: value } } })
// 示例:查找年龄不小于 25 的用户
db.users.find({ age: { $not: { $lt: 25 } } })
$exists
- 字段存在
db.collection.find({ field: { $exists: true or false } })
// 示例:查找有 `email` 字段的用户
db.users.find({ email: { $exists: true } })
更新操作符(Update Operators)
$set
- 设置字段的值
db.collection.update({ query }, { $set: { field: value } })
// 示例:更新名字为 "Tom" 的用户的年龄为 30
db.users.update({ name: "Tom" }, { $set: { age: 30 } })
$unset
- 删除字段
db.collection.update({ query }, { $unset: { field: "" } })
// 示例:删除名字为 "Tom" 的用户的 `age` 字段
db.users.update({ name: "Tom" }, { $unset: { age: "" } })
$inc
- 增加数值字段的值
db.collection.update({ query }, { $inc: { field: value } })
// 示例:将名字为 "Tom" 的用户的年龄增加 2
db.users.update({ name: "Tom" }, { $inc: { age: 2 } })
$push
- 向数组字段添加元素
db.collection.update({ query }, { $push: { field: value } })
// 示例:向名字为 "Tom" 的用户的兴趣列表添加 "足球"
db.users.update({ name: "Tom" }, { $push: { interests: "足球" } })
$pull
- 从数组字段删除元素
db.collection.update({ query }, { $pull: { field: value } })
// 示例:从名字为 "Tom" 的用户的兴趣列表中删除 "足球"
db.users.update({ name: "Tom" }, { $pull: { interests: "足球" } })
$addToSet
- 向数组添加元素,但不创建重复项
db.collection.update({ query }, { $addToSet: { field: value } })
// 示例:向名字为 "Tom" 的用户的兴趣列表中添加 "游泳",如果 "游泳" 已存在,则忽略
db.users.update({ name: "Tom" }, { $addToSet: { interests: "游泳" } })
$each
- 与 $push
或 $addToSet
配合,向数组添加多个元素
db.collection.update({ query }, { $push: { field: { $each: [value1, value2] } } })
// 示例:一次性向名字为 "Tom" 的用户的兴趣列表中添加多个兴趣
db.users.update({ name: "Tom" }, { $push: { interests: { $each: ["绘画", "舞蹈"] } } })
$pop
- 从数组的开头或末尾删除元素
// $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
- 过滤数据
db.collection.aggregate([ { $match: { field: value } } ])
// 示例:筛选所有年龄大于 25 的用户
db.users.aggregate([ { $match: { age: { $gt: 25 } } }])
$group
- 按字段分组
db.collection.aggregate([
{ $group: { _id: "$field", count: { $sum: 1 } } }
])
// 示例:按兴趣分组计数用户
db.users.aggregate([
{ $group: { _id: "$interests", count: { $sum: 1 } } }
])
$project
- 指定输出字段
db.collection.aggregate([ { $project: { field1: 1, field2: 0 } } ])
// 示例:输出用户的名字和兴趣,不输出其他字段
db.users.aggregate([ { $project: { name: 1, interests: 1 } }])
$sort
- 排序
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
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
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
db.collection.find({ field: { $gte: value } })
// Example: Find all users aged 25 or older
db.users.find({ age: { $gte: 25 } })
$lt
- Less Than
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
db.collection.find({ field: { $lte: value } })
// Example: Find all users aged 25 or younger
db.users.find({ age: { $lte: 25 } })
$ne
- Not Equal
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
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
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
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
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
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
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
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
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
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
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
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
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
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
// $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
db.collection.aggregate([ { $match: { field: value } } ])
// Example: Filter all users older than 25
db.users.aggregate([ { $match: { age: { $gt: 25 } } }])
$group
- Group by Field
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
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
db.collection.aggregate([ { $sort: { field: 1 or -1 } } ])
// 1 for ascending order, -1 for descending
转载自:https://juejin.cn/post/7323271665945444389