当前位置: 代码网 > it编程>数据库>Nosql > Mongodb常见操作符和运算符总结

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

2024年05月15日 Nosql 我要评论
查询操作符(query operators)$eq - 等于db.collection.find({ field: { $eq: value } })// 示例:查找所有名字等于 "tom" 的文档d

查询操作符(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)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com