Mongodb與Mysql的查詢指令碼操作對比

2021-06-27 13:24:15 字數 2493 閱讀 2766

1、查詢所有記錄

db.userinfo.find();

相當於:select * from userinfo;

但是你可以設定每頁顯示資料的大小,用dbquery.shellbatchsize = 50;這樣每頁就顯示50條記錄了。

2、查詢去掉後的當前聚集集合中的某列的重複資料

db.userinfo.distinct(「name」);

會過濾掉name中的相同資料

相當於:select distict name from userinfo;

3、查詢age = 22的記錄

db.userinfo.find();

相當於:select * from userinfo where age = 22;

4、查詢age > 22的記錄

db.userinfo.find(});

相當於:select * from userinfo where age > 22;

5、查詢age < 22的記錄

db.userinfo.find(});

相當於:select * from userinfo where age < 22;

6、查詢age >= 25的記錄

db.userinfo.find(});

相當於:select * from userinfo where age >= 25;

7、查詢age <= 25的記錄

db.userinfo.find(});

8、查詢age >= 23 並且age <= 26

db.userinfo.find(});

9、查詢name中包含mongo的資料

db.userinfo.find();

//相當於%%

select * from userinfo where name like 『%mongo%』;

10、查詢name中以mongo開頭的

db.userinfo.find();

select * from userinfo where name like 『mongo%』;

11、查詢指定列name、age資料

db.userinfo.find({}, );

相當於:select name, age from userinfo;

當然name也可以用true或false,當用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列資訊。

12、查詢指定列name、age資料, age > 25

db.userinfo.find(}, );

相當於:select name, age from userinfo where age > 25;

13、按照年齡排序

公升序:db.userinfo.find().sort();

降序:db.userinfo.find().sort();

14、查詢name = zhangsan, age = 22的資料

db.userinfo.find();

相當於:select * from userinfo where name = 『zhangsan』and age = 『22』;

15、查詢前5條資料

db.userinfo.find().limit(5);

相當於:select top 5 * from userinfo;

16、查詢10條以後的資料

db.userinfo.find().skip(10);

相當於:select * from userinfo where id not in (

select top 10 * from userinfo

);17、查詢在5-10之間的資料

db.userinfo.find().limit(10).skip(5);

可用於分頁,limit是pagesize,skip是第幾頁*pagesize

18、or與 查詢

db.userinfo.find(, ]});

相當於:select * from userinfo where age = 22 or age = 25;

19、and與 查詢

db.userinfo.find(, ]});

相當於:select * from userinfo where age = 22 and name = 223;

20、查詢第一條資料

db.userinfo.findone();

相當於:select top 1 * from userinfo;

db.userinfo.find().limit(1);

21、查詢某個結果集的記錄條數

db.userinfo.find(}).count();

相當於:select count(*) from userinfo where age >= 20;

22、按照某列進行排序

db.userinfo.find(}).count();

相當於:select count(***) from userinfo

mongoDB與mysql的區別

資料庫模型 非關係性資料庫 關係型資料庫 儲存方式 虛擬記憶體 持久化 持久化 查詢語句 獨特的 mongodb查詢方式 傳統sql語句查詢 資料處理方式 基於記憶體,將熱資料存在物理記憶體中,從而達到高速讀寫 io讀取 mongodb優點 快速 擁有適量級記憶體的mongodb的效能是非常迅速的,...

mysql 與mongodb的特點與優劣

介紹 mongodb是乙個基於分布式檔案儲存的資料庫。高效能,開源,無模式的文件型資料庫,為web應用提供可擴充套件的高效能資料儲存,是當前nosql資料庫中比較熱門的一種,由c 語言編寫。mongodb是乙個介於關聯式資料庫和非關聯式資料庫之間的產品,是非關聯式資料庫當中功能最豐富,最像關聯式資料...

MongoDB增刪查改

mongodb沒有建立資料庫的命令,但是你可以先執行use db name來進行一些操作,如db.createcollection db table 這樣就可以建立乙個db name的資料庫了。以下語句其實都不用加引號 insert方法 insert obj db.test.insert write...