MongoDB的基礎(chǔ)查詢和索引操作方法總結(jié)
查詢操作
1、查詢所有記錄
db.userInfo.find();
相當于:
select* from userInfo;
2、查詢?nèi)サ艉蟮漠斍熬奂现械哪沉械闹貜?fù)數(shù)據(jù)
db.userInfo.distinct("name");
會過濾掉name中的相同數(shù)據(jù)
相當于:
select disttince 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 <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});
相當于:
select * from userInfo where age >=23 and age <= 26;
9、查詢name中包含 mongo的數(shù)據(jù)
db.userInfo.find({name: /mongo/});
相當于:
select * from userInfo where name like ‘%mongo%';
10、查詢name中以mongo開頭的
db.userInfo.find({name: /^mongo/});
相當于:
select * from userInfo where name like ‘mongo%';
11、查詢指定列name、age數(shù)據(jù)
db.userInfo.find({}, {name: 1, age: 1});
相當于:
select name, age from userInfo;
當然name也可以用true或false,當用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列信息。
12、查詢指定列name、age數(shù)據(jù), age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相當于:
select name, age from userInfo where age >25;
13、按照年齡排序
升序:
db.userInfo.find().sort({age: 1});
降序:
db.userInfo.find().sort({age: -1});
14、查詢前5條數(shù)據(jù)
db.userInfo.find().limit(5);
相當于:
select * from (select * from userInfo) where rownum < 6;//oracle select * from userInfo limit 5;//mysql
15、查詢10條以后的數(shù)據(jù)
db.userInfo.find().skip(10);
相當于:
select * from userInfo where id not in (select id from (select * from userInfo) where and rownum < 11);
db.userInfo.find().limit(10).skip(5);
可用于分頁,limit是pageSize,skip是第幾頁*pageSize
17、or與 查詢
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相當于:
select * from userInfo where age = 22 or age = 25;
18、查詢第一條數(shù)據(jù)
db.userInfo.findOne(); db.userInfo.find().limit(1);
相當于:
select * from (select * from userInfo) where and rownum < 2
19、查詢某個結(jié)果集的記錄條數(shù)
db.userInfo.find({age: {$gte: 25}}).count(); 相當于:select count(*) from userInfo where age >= 20;
索引
1、創(chuàng)建索引
db.userInfo.ensureIndex({username: 1});
在MongoDB中,我們同樣可以創(chuàng)建復(fù)合索引,如下:
db.userInfo.ensureIndex({username: 1, age: -1});
該索引被創(chuàng)建后,基于username和age的查詢將會用到該索引,或者是基于username的查詢也會用到該索引,但是只是基于age的查詢將不會用到該復(fù)合索引。因此可以說,如果想用到復(fù)合索引,必須在查詢條件中包含復(fù)合索引中的前N個索引列。然而如果查詢條件中的鍵值順序和復(fù)合索引中的創(chuàng)建順序不一致的話,MongoDB可以智能的幫助我們調(diào)整該順序,以便使復(fù)合索引可以為查詢所用。如:
db.test.find({"age": 30, "username": "stephen"})
對于上面示例中的查詢條件,MongoDB在檢索之前將會動態(tài)的調(diào)整查詢條件文檔的順序,以使該查詢可以用到剛剛創(chuàng)建的復(fù)合索引。
2、創(chuàng)建唯一索引
在缺省情況下創(chuàng)建的索引均不是唯一索引。下面的示例將創(chuàng)建唯一索引,如:
db.test.ensureIndex({"userid":1},{"unique":true})
如果再次插入userid重復(fù)的文檔時,MongoDB將報錯,以提示插入重復(fù)鍵,如:
db.test.insert({"userid":5}) db.test.insert({"userid":5})
E11000 duplicate key error index: test.test.$userid_1 dup key: { : 5.0 }
如果插入的文檔中不包含userid鍵,那么該文檔中該鍵的值為null,如果多次插入類似的文檔,MongoDB將會報出同樣的錯誤,如:
db.test.insert({"userid1":5}) db.test.insert({"userid1":5})
E11000 duplicate key error index: test.test.$userid_1 dup key: { : null }
如果在創(chuàng)建唯一索引時已經(jīng)存在了重復(fù)項,我們可以通過下面的命令幫助我們在創(chuàng)建唯一索引時消除重復(fù)文檔,僅保留發(fā)現(xiàn)的第一個文檔,如:
--先刪除剛剛創(chuàng)建的唯一索引。
db.test.dropIndex({"userid":1})
--插入測試數(shù)據(jù),以保證集合中有重復(fù)鍵存在。
db.test.remove() db.test.insert({"userid":5}) db.test.insert({"userid":5})
--創(chuàng)建唯一索引,并消除重復(fù)數(shù)據(jù)。
db.test.ensureIndex({"userid":1},{"unique":true,"dropDups":true})
--查詢結(jié)果確認,重復(fù)的鍵確實在創(chuàng)建索引時已經(jīng)被刪除。
db.test.find() { "_id" : ObjectId("4fe823c180144abd15acd52e"), "userid" : 5 }
我們同樣可以創(chuàng)建復(fù)合唯一索引,即保證復(fù)合鍵值唯一即可。如:
db.test.ensureIndex({"userid":1,"age":1},{"unique":true})
3、查詢當前聚集集合所有索引
db.userInfo.getIndexes();
4、查看總索引記錄大小
db.userInfo.totalIndexSize();
5、讀取當前集合的所有index信息
db.users.reIndex();
6、刪除指定索引
db.users.dropIndex("username":1);
7、刪除所有索引索引
db.users.dropIndexes();
相關(guān)文章
詳解MongoDB中創(chuàng)建集合與刪除集合的操作方法
因為MongoDB屬于NoSQL,所以集合collection相當于關(guān)系型數(shù)據(jù)庫中的表table,這里我們就來詳解MongoDB中創(chuàng)建集合與刪除集合的操作方法:2016-06-06Linux系統(tǒng)下MongoDB的簡單安裝與基本操作
這篇文章主要介紹了Linux系統(tǒng)下MongoDB的簡單安裝與基本操作,需要的朋友可以參考下2015-04-04MongoDB4.0在windows10下的安裝與服務(wù)配置教程詳解
本文通過圖文并茂的形式給大家介紹了MongoDB4.0在windows10下的安裝與服務(wù)配置教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08MongoDB詭異問題之sh.stopBalancer卡住的解決方法
這篇文章主要給大家介紹了關(guān)于MongoDB詭異問題之sh.stopBalancer卡住解決的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法
MongoDB?是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。接下來通過本文給大家介紹MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法,感興趣的朋友一起看看吧2021-11-11