一、MongoDB 數(shù)據(jù)庫(kù)常用操作命令
1、Help查看命令提示
1
2
3
|
help db.help(); db.yourColl.help(); |
1
|
use raykaeso; |
當(dāng)創(chuàng)建一個(gè)集合(table)的時(shí)候會(huì)自動(dòng)創(chuàng)建當(dāng)前數(shù)據(jù)庫(kù)
3、查詢所有數(shù)據(jù)庫(kù)
1
|
show dbs; |
1
|
db.dropDatabase(); |
1
|
db.cloneDatabase(“127.0.0.1”); |
將指定機(jī)器上的數(shù)據(jù)庫(kù)的數(shù)據(jù)克隆到當(dāng)前數(shù)據(jù)庫(kù)
6、從指定的機(jī)器上復(fù)制指定數(shù)據(jù)庫(kù)數(shù)據(jù)到某個(gè)數(shù)據(jù)庫(kù)
1
|
db.copyDatabase(“mydb”, “temp”, “127.0.0.1”); |
將本機(jī)的mydb的數(shù)據(jù)復(fù)制到temp數(shù)據(jù)庫(kù)中
7、修復(fù)當(dāng)前數(shù)據(jù)庫(kù)
1
|
db.repairDatabase(); |
1
|
db.getName() /db ; |
1
|
db.stats(); |
1
|
db.version(); |
1
|
db.getMongo(); |
1
2
3
|
db.getPrevError(); db.resetError(); |
二、MongoDB Collection聚集集合
1、創(chuàng)建一個(gè)聚集集合(table)
1
2
3
|
db.createCollection(“collName”, {size: 20, capped: 5, max: 100}); // 創(chuàng)建成功會(huì)顯示{“ok”:1} // 判斷集合是否為定容量db.collName.isCapped(); |
1
|
db.getCollection(“account”); |
1
|
db.getCollectionNames(); |
1
|
db.printCollectionStats(); |
1
|
db.yourColl.count(); |
1
|
db.yourColl.dataSize(); |
1
|
db.yourColl.getDB(); |
1
|
db.coll.stats(); |
1
|
db.coll.totalSize(); |
1
|
db.coll.storageSize(); |
1
|
db.coll.renameCollection(“ray”); |
將coll重命名為ray
12、刪除當(dāng)前聚集集合
1
|
db.coll.drop(); |
三、MongoDB用戶相關(guān)
1、添加一個(gè)用戶(創(chuàng)建)
1
|
db.createUser({user: 'username' , pwd : 'xxxx' , roles: [{role: 'readWrite' , db: 'dbname' }]}); |
添加用戶、設(shè)置密碼、是否只讀
2、數(shù)據(jù)庫(kù)認(rèn)證、安全模式(登錄)
1
|
db.auth(“ray”, “123456”); |
1
|
show users ; |
1
|
db.removeUser(“userName”); |
四、MongoDB聚集集合查詢
1、查詢所有記錄
1
|
db.userInfo. find (); |
相當(dāng)于:select* from userInfo;
默認(rèn)每頁(yè)顯示20條記錄,當(dāng)顯示不下的情況下,可以用it迭代命令查詢下一頁(yè)數(shù)據(jù)。注意:鍵入it命令不能帶“;”
但是你可以設(shè)置每頁(yè)顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁(yè)就顯示50條記錄了。
2、查詢?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù)
1
|
db.userInfo.distinct(“name”); |
會(huì)過(guò)濾掉name中的相同數(shù)據(jù)
相當(dāng)于:select distict name from userInfo;
3、查詢age = 22的記錄
1
|
db.userInfo. find ({“age”: 22}); |
相當(dāng)于: select * from userInfo where age = 22;
4、條件查詢的記錄MongoDB中條件操作符有:
(>) 大于 – $gt
(<) 小于 – $lt (>=) 大于等于 – $gte
(<= ) 小于等于 – $lte
1
2
3
4
5
6
7
8
|
db.userInfo. find ({age: {$gt: 22}}); 相當(dāng)于: select * from userInfo where age>22; db.userInfo. find ({age: {$lt: 22}}); 相當(dāng)于: select * from userInfo where age<22; db.userInfo. find ({age: {$gte: 25}}); 相當(dāng)于: select * from userInfo where age >= 25; |
1
2
3
4
|
db.userInfo. find ({name: /mongo/ }); // 相當(dāng)于%% select * from userInfo where name like ‘%mongo%'; |
1
2
3
|
db.userInfo. find ({}, {name: 1, age: 1}); 相當(dāng)于: select name, age from userInfo; |
當(dāng)然name也可以用true或false
8、按條件查詢指定列數(shù)據(jù)
1
2
3
|
db.userInfo. find ({age: {$gt: 25}}, {name: 1, age: 1}); 相當(dāng)于: select name, age from userInfo where age <25; |
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
10、查詢前5條數(shù)據(jù)
1
2
3
|
db.userInfo. find ().limit(5); 相當(dāng)于: select * from userInfo limit 5; |
1
2
3
4
5
|
db.userInfo. find ().skip(10); 相當(dāng)于: select count() from userInfo as total; select from userInfo limit 10,total; |
1
|
db.userInfo. find ().limit(10).skip(5); |
可用于分頁(yè),limit是pageSize,skip是第幾頁(yè)pageSize
相當(dāng)于:select from userInfo limit 5,10;
13、or與 查詢
1
2
3
|
db.userInfo. find ({$or: [{age: 22}, {age: 25}]}); 相當(dāng)于: select * from userInfo where age = 22 or age = 25; |
1
2
3
4
5
|
db.userInfo.findOne(); db.userInfo. find ().limit(1); 相當(dāng)于: select * from userInfo limit 1; |
1
2
3
|
db.userInfo. find ({age: {$gte: 25}}).count(); 相當(dāng)于: select count(*) from userInfo where age >= 20; |
五、MongoDB索引
1、創(chuàng)建索引
1
2
3
|
db.userInfo.ensureIndex({name: 1}); db.userInfo.ensureIndex({name: 1, ts: -1}); |
1
|
db.userInfo.getIndexes(); |
1
|
db.userInfo.totalIndexSize(); |
1
|
db. users .reIndex(); |
1
|
db. users .dropIndex(“name_1″); |
1
|
db. users .dropIndexes(); |
六、MongoDB修改、添加、刪除集合數(shù)據(jù)
1、添加
1
|
db. users .save({name: ‘zhangsan', age: 25, sex: true }); |
添加的數(shù)據(jù)的數(shù)據(jù)列,沒(méi)有固定,根據(jù)添加的數(shù)據(jù)為準(zhǔn)
2、修改
1
2
3
4
5
6
7
8
|
db. users .update({age: 25}, {$ set : {name: ‘changeName'}}, false , true ); 相當(dāng)于:update users set name = ‘changeName' where age = 25; db. users .update({name: ‘Lisi'}, {$inc: {age: 50}}, false , true ); 相當(dāng)于:update users set age = age + 50 where name = ‘Lisi'; db. users .update({name: ‘Lisi '}, {$inc: {age: 50}, $set: {name: ‘hoho' }}, false , true ); 相當(dāng)于:update users set age = age + 50, name = ‘hoho ' where name = ‘Lisi' ; |
1
|
db. users .remove({age: 132}); |
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 }); |
更多關(guān)于MongoDB常用數(shù)據(jù)庫(kù)命令文章請(qǐng)查看下面的相關(guān)鏈接原文鏈接:http://www.cnblogs.com/luoahong/