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

