MongoDB數(shù)據(jù)庫(kù)常用28條查詢語(yǔ)句總結(jié)
初始MongoDB
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由 C++ 語(yǔ)言編寫(xiě)。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。
- database:數(shù)據(jù)庫(kù),SQL中的database含義。
- collection:數(shù)據(jù)庫(kù)表/集合,SQL中的table含義。
- document:數(shù)據(jù)記錄行/文檔,SQL中的row含義。
- field:數(shù)據(jù)字段/域,SQL中的column含義。
- index:索引,SQL中的index含義。
- primary key:主鍵,MongoDB自動(dòng)將_id字段設(shè)置為主鍵,SQL中的primary key含義。
MongoDB特點(diǎn)
- MongoDB 是一個(gè)面向文檔存儲(chǔ)的數(shù)據(jù)庫(kù),操作起來(lái)比較簡(jiǎn)單和容易。
- 你可以在MongoDB記錄中設(shè)置任何屬性的索引 (如:FirstName="Sameer",Address="8 Gandhi Road")來(lái)實(shí)現(xiàn)更快的排序。
- 你可以通過(guò)本地或者網(wǎng)絡(luò)創(chuàng)建數(shù)據(jù)鏡像,這使得MongoDB有更強(qiáng)的擴(kuò)展性。
- 如果負(fù)載的增加(需要更多的存儲(chǔ)空間和更強(qiáng)的處理能力) ,它可以分布在計(jì)算機(jī)網(wǎng)絡(luò)中的其他節(jié)點(diǎn)上這就是所謂的分片。
- Mongo支持豐富的查詢表達(dá)式。查詢指令使用JSON形式的標(biāo)記,可輕易查詢文檔中內(nèi)嵌的對(duì)象及數(shù)組。
- MongoDb 使用update()命令可以實(shí)現(xiàn)替換完成的文檔(數(shù)據(jù))或者一些指定的數(shù)據(jù)字段 。
- Mongodb中的Map/reduce主要是用來(lái)對(duì)數(shù)據(jù)進(jìn)行批量處理和聚合操作。
- Map和Reduce。Map函數(shù)調(diào)用emit(key,value)遍歷集合中所有的記錄,將key與value傳給Reduce函數(shù)進(jìn)行處理。
- Map函數(shù)和Reduce函數(shù)是使用Javascript編寫(xiě)的,并可以通過(guò)db.runCommand或mapreduce命令來(lái)執(zhí)行MapReduce操作。
- GridFS是MongoDB中的一個(gè)內(nèi)置功能,可以用于存放大量小文件。
- MongoDB允許在服務(wù)端執(zhí)行腳本,可以用Javascript編寫(xiě)某個(gè)函數(shù),直接在服務(wù)端執(zhí)行,也可以把函數(shù)的定義存儲(chǔ)在服務(wù)端,下次直接調(diào)用即可。
- MongoDB支持各種編程語(yǔ)言:RUBY,PYTHON,JAVA,C++,PHP,C#等多種語(yǔ)言。
- MongoDB安裝簡(jiǎn)單。
1、查詢所有記錄
db.userInfo.find();
相當(dāng)于:
select* from userInfo;
默認(rèn)每頁(yè)顯示20條記錄,當(dāng)顯示不下的情況下,可以用it迭代命令查詢下一頁(yè)數(shù)據(jù)。注意:鍵入it命令不能帶“;”
但是你可以設(shè)置每頁(yè)顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁(yè)就顯示50條記錄了。
2、查詢?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù)
db.userInfo.distinct("name");
會(huì)過(guò)濾掉 name 中的相同數(shù)據(jù)
相當(dāng)于:
select distict name from userInfo;
3、查詢 age = 22 的記錄
db.userInfo.find({"age": 22});
相當(dāng)于:
select * from userInfo where age = 22;
4、查詢 age > 22 的記錄
db.userInfo.find({age: {$gt: 22}});
相當(dāng)于:
select * from userInfo where age > 22;
5、查詢 age < 22 的記錄
db.userInfo.find({age: {$lt: 22}});
相當(dāng)于:
select * from userInfo where age < 22;
6、查詢 age >= 25 的記錄
db.userInfo.find({age: {$gte: 25}});
相當(dāng)于:
select * from userInfo where age >= 25;
7、查詢 age <= 25 的記錄
db.userInfo.find({age: {$lte: 25}});
相當(dāng)于:
select * from userInfo where age <= 25;
8、查詢 age >= 23 并且 age <= 25 注意書(shū)寫(xiě)格式
db.userInfo.find({age: {$gte: 23, $lte: 25}});
相當(dāng)于:
select * from userInfo where age>=23 and age <= 25;
9、查詢 age != 25 的記錄
db.userInfo.find({age: {$ne: 25}});
相當(dāng)于:
select * from userInfo where age != 25;
10、查詢 name 中包含 mongo 的數(shù)據(jù) 模糊查詢用于搜索
db.userInfo.find({name: /mongo/});
相當(dāng)于:
select * from userInfo where name like '%mongo%';
11、查詢 name 中以 mongo 開(kāi)頭的
db.userInfo.find({name: /^mongo/});
相當(dāng)于:
select * from userInfo where name like 'mongo%';
12、查詢 name 中以 mongo 結(jié)尾的
db.userInfo.find({name: /mongo$/});
相當(dāng)于:
select * from userInfo where name like ‘%mongo';
模糊查詢語(yǔ)法:{ : /pattern/ }
其中options值可以為:
- i -- 不區(qū)分大小寫(xiě)。
- m -- 匹配value中有換行符(\n)的情形,還有一個(gè)情形是:匹配規(guī)則中使用了錨,所謂的錨就是^ 開(kāi)頭, $ 結(jié)尾。
- s -- 允許點(diǎn)字符(.)匹配所有的字符,包括換行符。
- x -- 忽視所有空白字符。
13、查詢指定列 name、age 數(shù)據(jù)
db.userInfo.find({}, {name: 1, age: 1});
相當(dāng)于:
select name, age from userInfo;
當(dāng)然 name 也可以用 true 或 false,當(dāng)用 ture 的情況下和 name:1 效果一樣,如果用 false 就是排除 name,顯示 name 以外的列信息。
14、查詢指定列 name、age 數(shù)據(jù), age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相當(dāng)于:
select name, age from userInfo where age > 25;
15、按照年齡排序 1 升序 -1 降序
升序:
db.userInfo.find().sort({age: 1});
相當(dāng)于:
select * from userInfo order by age asc;
降序:
db.userInfo.find().sort({age: -1});
相當(dāng)于:
select * from userInfo order by age desc;
17、查詢前 5 條數(shù)據(jù)
db.userInfo.find().limit(5);
相當(dāng)于:
select * from userInfo limit 5;
18、查詢 10 條以后的數(shù)據(jù)
db.userInfo.find().skip(10);
19、查詢?cè)?6-10條 之間的數(shù)據(jù)
db.userInfo.find().limit(10).skip(5);
可用于分頁(yè),limit 是 pageSize,第n頁(yè)時(shí) skip 是 (n-1)*pageSize
相當(dāng)于:
select * from userInfo limit 5,5;
20、and 查詢 name = zhangsan, age = 22 的數(shù)據(jù)
db.userInfo.find({name: 'zhangsan', age: 22});
相當(dāng)于:
select * from userInfo where name = 'zhangsan' and age = 22;
21、or 查詢
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相當(dāng)于:
select * from userInfo where age = 22 or age = 25;
注意多條件間用中括號(hào)[]包圍。
22、in 查詢
db.userInfo.find({age :{$in:[22,25]}});
相當(dāng)于:
select * from userInfo where age in (22,25);
23、查詢某個(gè)結(jié)果集的記錄條數(shù)
統(tǒng)計(jì)數(shù)量
db.userInfo.find({age: {$gte: 25}}).count();
相當(dāng)于:
select count(*) from userInfo where age >= 20;
skip(), limilt(), sort()三個(gè)放在一起執(zhí)行的時(shí)候,執(zhí)行的順序是先 sort(), 然后是 skip(),最后是顯示的 limit()。
24、查詢某個(gè)時(shí)間段的數(shù)據(jù)(時(shí)間為日期類型,非字符串類型)
db.userInfo.find({createTime:{$gt:ISODate("2020-11-09T00:00:00Z")}});
相當(dāng)于:
select * from userInfo where createTime> '2020-11-09 00:00:00';
25、對(duì)表中一字段進(jìn)行統(tǒng)計(jì)求和
db.userInfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})
相當(dāng)于:
SELECT SUM(score) from userInfo;
26、對(duì)表中一字段進(jìn)行統(tǒng)計(jì)求平均值
db.userInfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})
相當(dāng)于:
SELECT AVG(score) from userInfo;
27、對(duì)表中指定條件記錄中的某字段求和
db.userInfo.aggregate({$match:{createTime:{$gte:ISODate("2020-11-10T00:00:00Z"),$lt:ISODate("2020-11-11T00:00:00Z")}}},{$group:{_id:null,score:{$sum:"$score"}}})
相當(dāng)于:
SELECT SUM(score) from userInfo where createTime >= '2020-11-10 00:00:00' and createTime < '2020-11-11 00:00:00';
28、根據(jù)A表,匹配B表所有滿足條件的集合,如根據(jù)用戶表userInfo表中的userId字段找出userAdress表中所有地址的集合,其中userId也為userAdress中的字段。
假設(shè) 有 用戶集合, 存儲(chǔ)的測(cè)試數(shù)據(jù) 如下:
db.userInfo.insert([ { "_id" : 1, "userId" : "xxxx", "username" : "ruink", "website" : "www.51ste.com" }, { "_id" : 2, "userId" : "yyyy", "username" : "foosingy", "website" : "www.scgossip.com" } ])
假設(shè) 有 地址集合, 存儲(chǔ)的測(cè)試數(shù)據(jù) 如下:
db.userAdress.insert([ { "_id" : 1, "userId" : "xxxx", address: "測(cè)試地址1"}, { "_id" : 2, "userId" : "yyyy", address: "測(cè)試地址2"}, { "_id" : 3, "userId" : "xxxx", address: "測(cè)試地址3"}, ])
查詢語(yǔ)句:
db.userInfo.aggregate([ { $lookup: { from: "userAdress", localField: "userId", foreignField: "userId", as: "address_detail" } }, { $match : {"userId" :"xxxx"} } ])
上表為找出userId="xxxx"的所有地址的集合,查詢結(jié)果如下:
[ { _id: 1, userId: 'xxxx', username: 'ruink', website:'www.51ste.com', address_docs: [ { _id: 1, userId: 'xxxx', address: '測(cè)試地址1' }, { _id: 3, userId: 'xxxx', address: '測(cè)試地址3' } ] } ]
總結(jié)
到此這篇關(guān)于MongoDB數(shù)據(jù)庫(kù)常用28條查詢語(yǔ)句總結(jié)的文章就介紹到這了,更多相關(guān)MongoDB常用查詢語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB中MapReduce編程模型使用實(shí)例
作為一個(gè)優(yōu)秀的編程模型,MapReduce在大數(shù)據(jù)處理中有很大的優(yōu)勢(shì),而mongodb也支持這一編程模型,本文通過(guò)簡(jiǎn)單的單詞計(jì)數(shù)示例論述在mongodb中如何使用MapReduce2014-04-04MongoDB4.28開(kāi)啟權(quán)限認(rèn)證配置用戶密碼登錄功能
這篇文章主要介紹了MongoDB4.28開(kāi)啟權(quán)限認(rèn)證配置用戶名和密碼認(rèn)證登錄,本文分步驟給大家介紹開(kāi)啟認(rèn)證登錄的方法,需要的朋友可以參考下2022-01-01使用aggregate在MongoDB中查詢重復(fù)數(shù)據(jù)記錄的方法
這篇文章主要介紹了使用aggregate在MongoDB中查詢重復(fù)數(shù)據(jù)記錄的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01關(guān)于Mongodb參數(shù)說(shuō)明與常見(jiàn)錯(cuò)誤處理的總結(jié)
這篇文章主要給大家介紹了關(guān)于Mongodb參數(shù)說(shuō)明與常見(jiàn)錯(cuò)誤處理的相關(guān)資料,文中通過(guò)一步步的步驟介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-07-07詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法
副本集是MongoDB的主從復(fù)制中的重要功能,經(jīng)常被用來(lái)作額外的備份,這里我們就來(lái)詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法,首先還是來(lái)回顧一下MongoDB中副本集的基本知識(shí):2016-07-07MongoDB聚合運(yùn)算符$dateFromString詳解
$dateFromString聚合運(yùn)算符將日期時(shí)間字符串轉(zhuǎn)換為日期對(duì)象,本文給大家介紹MongoDB聚合運(yùn)算符$dateFromString的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2024-03-03MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like)
這篇文章主要介紹了MongoDB的模糊查詢操作(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like) ,本文通過(guò)代碼案例分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,,需要的朋友可以參考下2019-07-07MongoDB數(shù)據(jù)庫(kù)文檔操作方法(必看篇)
下面小編就為大家?guī)?lái)一篇MongoDB數(shù)據(jù)庫(kù)文檔操作方法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07