Mongodb常見操作符和運(yùn)算符總結(jié)
查詢操作符(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 - 在數(shù)組中
db.collection.find({ field: { $in: array } }) // 示例:查找興趣包含 "閱讀" 或 "游泳" 的用戶 db.users.find({ interests: { $in: ["閱讀", "游泳"] } })
$nin - 不在數(shù)組中
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 - 設(shè)置字段的值
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 - 增加數(shù)值字段的值
db.collection.update({ query }, { $inc: { field: value } }) // 示例:將名字為 "Tom" 的用戶的年齡增加 2 db.users.update({ name: "Tom" }, { $inc: { age: 2 } })
$push - 向數(shù)組字段添加元素
db.collection.update({ query }, { $push: { field: value } }) // 示例:向名字為 "Tom" 的用戶的興趣列表添加 "足球" db.users.update({ name: "Tom" }, { $push: { interests: "足球" } })
$pull - 從數(shù)組字段刪除元素
db.collection.update({ query }, { $pull: { field: value } }) // 示例:從名字為 "Tom" 的用戶的興趣列表中刪除 "足球" db.users.update({ name: "Tom" }, { $pull: { interests: "足球" } })
$addToSet - 向數(shù)組添加元素,但不創(chuàng)建重復(fù)項(xiàng)
db.collection.update({ query }, { $addToSet: { field: value } }) // 示例:向名字為 "Tom" 的用戶的興趣列表中添加 "游泳",如果 "游泳" 已存在,則忽略 db.users.update({ name: "Tom" }, { $addToSet: { interests: "游泳" } })
$each - 與 $push 或 $addToSet 配合,向數(shù)組添加多個(gè)元素
db.collection.update({ query }, { $push: { field: { $each: [value1, value2] } } }) // 示例:一次性向名字為 "Tom" 的用戶的興趣列表中添加多個(gè)興趣 db.users.update({ name: "Tom" }, { $push: { interests: { $each: ["繪畫", "舞蹈"] } } })
$pop - 從數(shù)組的開頭或末尾刪除元素
// $pop: 1 從末尾移除,$pop: -1 從開頭移除 db.collection.update({ query }, { $pop: { field: 1 or -1 } }) // 示例:從名字為 "Tom" 的用戶的興趣列表末尾刪除一個(gè)興趣 db.users.update({ name: "Tom" }, { $pop: { interests: 1 } })
聚合管道操作符(Aggregation Pipeline Operators)
$match - 過濾數(shù)據(jù)
db.collection.aggregate([ { $match: { field: value } } ]) // 示例:篩選所有年齡大于 25 的用戶 db.users.aggregate([ { $match: { age: { $gt: 25 } } }])
$group - 按字段分組
db.collection.aggregate([ { $group: { _id: "$field", count: { $sum: 1 } } } ]) // 示例:按興趣分組計(jì)數(shù)用戶 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中的一些常用操作符和運(yùn)算符,它們可以幫你構(gòu)建靈活和強(qiáng)大的數(shù)據(jù)操作指令。在實(shí)際的應(yīng)用中,會(huì)將多個(gè)操作符組合起來使用,以滿足復(fù)雜的業(yè)務(wù)邏輯。
到此這篇關(guān)于Mongodb常見操作符和運(yùn)算符總結(jié)的文章就介紹到這了,更多相關(guān)Mongodb操作符和運(yùn)算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解MongoDB數(shù)據(jù)庫基礎(chǔ)操作及實(shí)例
這篇文章主要介紹了詳解MongoDB數(shù)據(jù)庫基礎(chǔ)操作及實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09mongodb數(shù)據(jù)庫的6個(gè)安全設(shè)置命令
這篇文章主要介紹了mongodb數(shù)據(jù)庫的6個(gè)安全設(shè)置命令,如安全模式啟動(dòng)、安全認(rèn)證、數(shù)據(jù)讀寫加鎖、解鎖等,需要的朋友可以參考下2014-05-05淺談MySQL和MariaDB區(qū)別(mariadb和mysql的性能比較)
MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品2018-02-02為MongoDB數(shù)據(jù)庫注冊windows服務(wù)
這篇文章介紹了為MongoDB數(shù)據(jù)庫注冊windows服務(wù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06MongoDB數(shù)據(jù)庫權(quán)限管理詳解
本文詳細(xì)講解了MongoDB數(shù)據(jù)庫權(quán)限管理的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07MongoDB 刪除文檔的方式(刪除一個(gè)、批量刪除)
這篇文章主要介紹了MongoDB 刪除文檔的方式(刪除一個(gè)、批量刪除),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04