mangodb查詢語句
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 -- 不區分大小寫。
m -- 匹配value中有換行符(\n)的情形,還有一個情形是:匹配規則中使用了錨,所謂的錨就是^ 開頭, $ 結尾。
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()三個放在一起執行的時候,執行的順序是先 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"},
])
查詢語句:

上表為找出userId="xxxx"的所有地址的集合,查詢結果如下:
復制

注意:
字段是什么類型,那么查詢時字段值的類型就應該是什么類型,比如如果字段類型是 NumberLog,那么查詢時就應該執行查詢類型為 NumberLog,如
轉自:https://blog.csdn.net/qq_41767116/article/details/125586683

浙公網安備 33010602011771號