3.MongoDB基本命令用法

                                        MongoDB基本命令用法
成功啓動MongoDB後,再打開一個命令行窗口輸入mongo,就能夠進行數據庫的一些操做。
輸入help能夠看到基本操做命令:

show dbs:顯示數據庫列表
show collections:顯示當前數據庫中的集合(相似關係數據庫中的表)
show users:顯示用戶

use <db name>:切換當前數據庫,這和MS-SQL裏面的意思同樣
db.help():顯示數據庫操做命令,裏面有不少的命令
db.foo.help():顯示集合操做命令,一樣有不少的命令,foo指的是當前數據庫下,一個叫foo的集合,並不是真正意義上的命令
db.foo.find():對於當前數據庫中的foo集合進行數據查找(因爲沒有條件,會列出全部數據)
db.foo.find( { a : 1 } ):對於當前數據庫中的foo集合進行查找,條件是數據中有一個屬性叫a,且a的值爲1

MongoDB沒有建立數據庫的命令,但有相似的命令。
如:若是你想建立一個「myTest」的數據庫,先運行use myTest命令,以後就作一些操做(如:db.createCollection('user')),這樣就能夠建立一個名叫「myTest」的數據庫。

數據庫經常使用命令

一、Help查看命令提示

 help

  db.help();

  db.yourColl.help();

  db.youColl.find().help();

  rs.help();
 
二、切換/建立數據庫

 use yourDB;  當建立一個集合(table)的時候會自動建立當前數據庫
 三、查詢全部數據庫

 show dbs;

四、刪除當前使用數據庫

 db.dropDatabase();

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

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

六、從指定的機器上覆制指定數據庫數據到某個數據庫

 db.copyDatabase("mydb", "temp", "127.0.0.1");將本機的mydb的數據複製到temp數據庫中

七、修復當前數據庫

 db.repairDatabase();

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

 db.getName();

 db; db和getName方法是同樣的效果,均可以查詢當前使用的數據庫

九、顯示當前db狀態

 db.stats();

十、當前db版本

 db.version();

十一、查看當前db的連接機器地址

 db.getMongo();

Collection彙集集合

一、建立一個彙集集合(table)

 db.createCollection(「collName」, {size: 20, capped: 5, max: 100});

二、獲得指定名稱的彙集集合(table)

 db.getCollection("account");

三、獲得當前db的全部彙集集合

 db.getCollectionNames();

四、顯示當前db全部彙集索引的狀態

 db.printCollectionStats();

 用戶相關
 一、添加一個用戶

 db.addUser("name");

 db.addUser("userName", "pwd123", true); 添加用戶、設置密碼、是否只讀

二、數據庫認證、安全模式

 db.auth("userName", "123123");

三、顯示當前全部用戶

 show users;

四、刪除用戶

 db.removeUser("userName");

其餘

一、查詢以前的錯誤信息

 db.getPrevError();

二、清除錯誤記錄

 db.resetError();
 查看彙集集合基本信息

一、查看幫助  db.yourColl.help();

二、查詢當前集合的數據條數  db.yourColl.count();

三、查看數據空間大小 db.userInfo.dataSize();

四、獲得當前彙集集合所在的db db.userInfo.getDB();

五、獲得當前彙集的狀態 db.userInfo.stats();

六、獲得彙集集合總大小 db.userInfo.totalSize();

七、彙集集合儲存空間大小 db.userInfo.storageSize();

八、Shard版本信息  db.userInfo.getShardVersion()

九、彙集集合重命名 db.userInfo.renameCollection("users"); 將userInfo重命名爲users

十、刪除當前彙集集合 db.userInfo.drop();

彙集集合查詢

一、查詢全部記錄
db.userInfo.find();

至關於:select* from userInfo;

默認每頁顯示20條記錄,當顯示不下的狀況下,能夠用it迭代命令查詢下一頁數據。注意:鍵入it命令不能帶「;」

可是你能夠設置每頁顯示數據的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。

 

二、查詢去掉後的當前彙集集合中的某列的重複數據

db.userInfo.distinct("name");

會過濾掉name中的相同數據

至關於:select distict name from userInfo;

 

三、查詢age = 22的記錄

db.userInfo.find({"age": 22});

至關於: select * from userInfo where age = 22;

 

四、查詢age > 22的記錄

db.userInfo.find({age: {$gt: 22}});

至關於:select * from userInfo where age >22;
五、查詢age < 22的記錄

db.userInfo.find({age: {$lt: 22}});

至關於:select * from userInfo where age <22;

 

六、查詢age >= 25的記錄

db.userInfo.find({age: {$gte: 25}});

至關於:select * from userInfo where age >= 25;

 

七、查詢age <= 25的記錄

db.userInfo.find({age: {$lte: 25}});

 

八、查詢age >= 23 而且 age <= 26

db.userInfo.find({age: {$gte: 23, $lte: 26}});

 

九、查詢name中包含 mongo的數據

db.userInfo.find({name: /mongo/});

//至關於%%

select * from userInfo where name like ‘%mongo%’;
十、查詢name中以mongo開頭的

db.userInfo.find({name: /^mongo/});

select * from userInfo where name like ‘mongo%’;

 

十一、查詢指定列name、age數據

db.userInfo.find({}, {name: 1, age: 1});

至關於:select name, age from userInfo;

固然name也能夠用true或false,當用ture的狀況下河name:1效果同樣,若是用false就是排除name,顯示name之外的列信息。

 

十二、查詢指定列name、age數據, age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

至關於:select name, age from userInfo where age >25;

 

1三、按照年齡排序

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

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

 

1四、查詢name = zhangsan, age = 22的數據

db.userInfo.find({name: 'zhangsan', age: 22});

至關於:select * from userInfo where name = ‘zhangsan’ and age = ‘22’;
1五、查詢前5條數據

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

至關於:selecttop 5 * from userInfo;

 

1六、查詢10條之後的數據

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

至關於:select * from userInfo where id not in (

selecttop 10 * from userInfo

);

 

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

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

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

 

1八、or與 查詢

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

至關於:select * from userInfo where age = 22 or age = 25;
1九、查詢第一條數據

db.userInfo.findOne();

至關於:selecttop 1 * from userInfo;

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

 

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

db.userInfo.find({age: {$gte: 25}}).count();

至關於:select count(*) from userInfo where age >= 20;

若是要返回限制以後的記錄數量,要使用count(true)或者count(非0)
db.users.find().skip(10).limit(5).count(true);

 

2一、按照某列進行排序

db.userInfo.find({sex: {$exists: true}}).count();

至關於:select count(sex) from userInfo;

索引

一、建立索引

db.userInfo.ensureIndex({name: 1});

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

 

二、查詢當前彙集集合全部索引

db.userInfo.getIndexes();

 

三、查看總索引記錄大小

db.userInfo.totalIndexSize();

 

四、讀取當前集合的全部index信息

db.users.reIndex();

 

五、刪除指定索引

db.users.dropIndex("name_1");

 

六、刪除全部索引索引

db.users.dropIndexes();

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

一、添加

db.users.save({name: ‘zhangsan’, age: 25, sex: true});

添加的數據的數據列,沒有固定,根據添加的數據爲準

 

二、修改

db.collection.update(criteria, objNew, upsert, multi )

criteria:update的查詢條件,相似sql update查詢內where後面的

objNew:update的對象和一些更新的操做符(如$,$inc...)等,也能夠理解爲sql update查詢內set後面的。

upsert : 若是不存在update的記錄,是否插入objNew,true爲插入,默認是false,不插入。

multi : mongodb默認是false,只更新找到的第一條記錄,若是這個參數爲true,就把按條件查出來多條記錄所有更新。

 

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’;

 

三、刪除

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

 

四、查詢修改刪除

db.users.findAndModify({

    query: {age: {$gte: 25}},

    sort: {age: -1},

    update: {$set: {name: 'a2'}, $inc: {age: 2}},

    remove: true

});

 

db.runCommand({ findandmodify : "users",

    query: {age: {$gte: 25}},

    sort: {age: -1},

    update: {$set: {name: 'a2'}, $inc: {age: 2}},

    remove: true

});
 sql

相關文章
相關標籤/搜索