mongodb:常用命令大全


一、MongoDB 數據庫常用操作命令

1、Help查看命令提示

1
2
3
help
db.help();
db.yourColl.help();

2、切換/創建數據庫

1
use raykaeso; 當創建一個集合(table)的時候會自動創建當前數據庫

3、查詢所有數據庫

1
show dbs;

4、刪除當前使用數據庫

1
db.dropDatabase();

5、從指定主機上克隆數據庫

1
db.cloneDatabase(“127.0.0.1”); 將指定機器上的數據庫的數據克隆到當前數據庫

6、從指定的機器上復制指定數據庫數據到某個數據庫

1
db.copyDatabase(“mydb”, “temp”, “127.0.0.1”);將本機的mydb的數據復制到temp數據庫中

7、修復當前數據庫

1
db.repairDatabase();

8、查看當前使用的數據庫

1
db.getName() /db ;

9、顯示當前db狀態

1
db.stats();

10、當前db版本

1
db.version();

11、查看當前db的連接服務器機器地址

1
db.getMongo();

12、查詢之前的錯誤信息和清除

1
2
db.getPrevError();
db.resetError();

二、MongoDB Collection聚集集合

1、創建一個聚集集合(table)

1
2
db.createCollection(“collName”, {size: 20, capped: 5, max: 100}); // 創建成功會顯示{“ok”:1}
// 判斷集合是否為定容量db.collName.isCapped();

2、得到指定名稱的聚集集合(table)

1
db.getCollection(“account”);

3、得到當前db的所有聚集集合

1
db.getCollectionNames();

4、顯示當前db所有聚集索引的狀態

1
db.printCollectionStats();

5、查詢當前集合的數據條數

1
db.yourColl.count();

6、查看當前集合數據空間大小

1
db.yourColl.dataSize();

7、得到當前聚集集合所在的db

1
db.yourColl.getDB();

8、得到當前聚集的狀態

1
db.coll.stats();

9、得到聚集集合總大小

1
db.coll.totalSize();

10、聚集集合儲存空間大小

1
db.coll.storageSize();

11、聚集集合重命名

1
db.coll.renameCollection(“ray”); 將coll重命名為ray

12、刪除當前聚集集合

1
db.coll.drop();

三、MongoDB用戶相關

1、添加一個用戶(創建)

1
db.createUser({user:  'username' pwd 'xxxx' , roles: [{role:  'readWrite' , db:  'dbname' }]}); 添加用戶、設置密碼、是否只讀

2、數據庫認證、安全模式(登錄)

1
db.auth(“ray”, “123456”);

3、顯示當前所有用戶

1
show  users ;

4、刪除用戶

1
db.removeUser(“userName”);

四、MongoDB聚集集合查詢

1、查詢所有記錄

1
2
db.userInfo. find ();
相當於: select * from userInfo;

默認每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁數據。注意:鍵入it命令不能帶“;”
但是你可以設置每頁顯示數據的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。

2、查詢去掉后的當前聚集集合中的某列的重復數據

1
2
3
db.userInfo.distinct(“name”);
會過濾掉name中的相同數據
相當於: select  distict name from userInfo;

3、查詢age = 22的記錄

1
2
db.userInfo. find ({“age”: 22});
相當於:  select  * from userInfo where age = 22;

4、條件查詢的記錄
MongoDB中條件操作符有:
(>) 大於 – $gt

(<) 小於 – $lt

(>=) 大於等於 – $gte

(<= ) 小於等於 – $lte

db.userInfo.find({age: {$gt: 22}}); 相當於:select * from userInfo where age>22;

1
2
3
4
5
6
7
db.userInfo. find ({age: {$lt: 22}});
相當於: select  * from userInfo where age<22; db.userInfo. find ({age: {$gte: 25}}); 相當於: select  * from userInfo where age >= 25;
 
db.userInfo. find ({age: {$lte: 25}});
相當於: select  * from userInfo where age <= 25; 5、and查詢 db.userInfo. find ({age: {$gte: 23, $lte: 26}}); 相當於: select  * from userInfo wher age >=23 and age <=26
db.userInfo. find ({name: ‘raykaeso’, age: 22});
相當於: select  * from userInfo where name = ‘raykaeso’ and age = ‘22′;

6、字符模糊查詢

1
2
3
db.userInfo. find ({name:  /mongo/ });
// 相當於%%
[code] select  * from userInfo where name like ‘%mongo%';

7、查詢指定列數據

1
2
3
db.userInfo. find ({}, {name: 1, age: 1});
相當於: select  name, age from userInfo;
當然name也可以用 true false

8、按條件查詢指定列數據

1
2
db.userInfo. find ({age: {$gt: 25}}, {name: 1, age: 1});
相當於: select  name, age from userInfo where age <25;

9、排序

1
2
升序:db.userInfo. find (). sort ({age: 1});
降序:db.userInfo. find (). sort ({age: -1});

10、查詢前5條數據

1
2
db.userInfo. find ().limit(5);
相當於: select  * from userInfo limit 5;

11、查詢10條以后的數據

1
2
3
db.userInfo. find ().skip(10);
相當於: select  count() from userInfo as total;
select  from userInfo limit 10,total;

12、查詢在5-10之間的數據

1
2
3
db.userInfo. find ().limit(10).skip(5);
可用於分頁,limit是pageSize,skip是第幾頁pageSize
相當於: select  from userInfo limit 5,10;

13、or與 查詢

1
2
db.userInfo. find ({$or: [{age: 22}, {age: 25}]});
相當於: select  * from userInfo where age = 22 or age = 25;

14、查詢第一條數據

1
2
3
db.userInfo.findOne();
db.userInfo. find ().limit(1);
相當於: select  * from userInfo limit 1;

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

1
2
db.userInfo. find ({age: {$gte: 25}}).count();
相當於: select  count(*) from userInfo where age >= 20;

五、MongoDB索引

1、創建索引

1
2
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});

2、查詢當前聚集集合所有索引

1
db.userInfo.getIndexes();

3、查看總索引記錄大小

1
db.userInfo.totalIndexSize();

4、讀取當前集合的所有index信息

1
db. users .reIndex();

5、刪除指定索引

1
db. users .dropIndex(“name_1″);

6、刪除所有索引索引

1
db. users .dropIndexes();

六、MongoDB修改、添加、刪除集合數據

1、添加

1
2
db. users .save({name: ‘zhangsan’, age: 25, sex:  true });
添加的數據的數據列,沒有固定,根據添加的數據為准

2、修改

1
2
3
4
5
6
db. users .update({age: 25}, {$ set : {name: ‘changeName’}},  false true );
相當於:update  users  set  name = ‘changeName’ where age = 25;
db. users .update({name: ‘Lisi’}, {$inc: {age: 50}},  false true );
相當於:update  users  set  age = age + 50 where name = ‘Lisi';
db. users .update({name: ‘Lisi’}, {$inc: {age: 50}, $ set : {name: ‘hoho’}},  false true );
相當於:update  users  set  age = age + 50, name = ‘hoho’ where name = ‘Lisi';

3、刪除

1
db. users .remove({age: 132});

4、查詢修改刪除

1
2
3
4
5
6
db. users .findAndModify({
query: {age: {$gte: 25}},
sort : {age: -1},
update: {$ set : {name: ‘a2′}, $inc: {age: 2}},
remove:  true
});
作者:羅阿紅 出處:http://www.cnblogs.com/luoahong/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM