Mongodb常见操作符和运算符总结

2024-03-01 0 983
目录
  • 查询操作符(Query Operators)
    • $eq – 等于
    • $gt – 大于
    • $gte – 大于等于
    • $lt – 小于
    • $lte – 小于等于
    • $ne – 不等于
    • $in – 在数组中
    • $nin – 不在数组中
    • $or – 或者
    • $and – 并且
    • $not – 非
    • $exists – 字段存在
  • 更新操作符(Update Operators)
    • $set – 设置字段的值
    • $unset – 删除字段
    • $inc – 增加数值字段的值
    • $push – 向数组字段添加元素
    • $pull – 从数组字段删除元素
    • $addToSet – 向数组添加元素,但不创建重复项
    • $each – 与 $push 或 $addToSet 配合,向数组添加多个元素
    • $pop – 从数组的开头或末尾删除元素
  • 聚合管道操作符(Aggregation Pipeline Operators)
    • $match – 过滤数据
    • $group – 按字段分组
    • $project – 指定输出字段
    • $sort – 排序

查询操作符(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中的一些常用操作符和运算符,它们可以帮你构建灵活和强大的数据操作指令。在实际的应用中,会将多个操作符组合起来使用,以满足复杂的业务逻辑。

到此这篇关于Mongodb常见操作符和运算符总结的文章就介绍到这了,更多相关Mongodb操作符和运算符内容请搜索悠久资源以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 MongoDB Mongodb常见操作符和运算符总结 https://www.u-9.cn/database/mongodb/182005.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务