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