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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows系統(tǒng)下安裝MongoDB與Robomongo環(huán)境詳解
這篇文章主要給大家介紹了在Windows系統(tǒng)下安裝MongoDB與Robomongo環(huán)境的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04
JavaScript按日期查詢MongoDB中的數(shù)據(jù)的要點(diǎn)示例
這篇文章主要介紹了JavaScript按日期查詢MongoDB中數(shù)據(jù)的要點(diǎn)示例,MongoDB所支持的BSON有JSON沒有的一些數(shù)據(jù)類型,如Date和BinData類型,需要的朋友可以參考下2016-03-03
MongoDB數(shù)據(jù)庫中索引(index)詳解
本文給大家詳細(xì)介紹了MongoDB數(shù)據(jù)庫中的索引的知識(shí),優(yōu)缺點(diǎn)以及使用技巧等方面,非常細(xì)致,有需要的小伙伴可以參考下2016-11-11
MongoDB入門教程之Windows下的MongoDB數(shù)據(jù)庫安裝圖解
這篇文章主要介紹了MongoDB入門教程之Windows下的MongoDB數(shù)據(jù)庫安裝圖解,本文還講解了MongoDB的基本操作,如insert、find、 update、remove等操作,需要的朋友可以參考下2014-08-08
ubuntu安裝mongodb創(chuàng)建賬號(hào)和庫及添加坐標(biāo)索引的流程分析
這篇文章主要介紹了ubuntu安裝mongodb創(chuàng)建賬號(hào)和庫及添加坐標(biāo)索引的流程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

