MongoDB的一些小結
強大 靈活 可擴展
1.擴展了關系型數據庫的輔助索引 范圍查詢(range query)和排序。
內置的對MapReduce式聚合的支持,以及對地理空間索引的支持。
2.面向文檔的數據庫,思路首先就是將原來的“行”(row)的概念換成更加靈活的“文檔”(documents)。面向文檔的方式可以將文檔或者數組內嵌進來,所以用一條記錄就可以表示非常復雜的層次關系。這對于面向對象語言的開發者來說,是非常自然的事情。
我們先來看看一些常見的操作命令
首先進入命令行之后默認的是test數據庫,一般而言我們首先需要切換到我們需要的數據庫,比如這里舉例一下 guazigz
use guazigz
再來詳細學習
一.數據庫常用命令
1.help查看命令提示
help
db.help();
db.yourColl.help();
db.youColl.find().help();
rs.help();
這里的youColl換成你數據庫里對應的集合(Collection)即可
2.查詢數據庫
show dbs;
3.刪除當前使用數據庫
db.dropDatabase();
4.從指定的主機上克隆數據庫
db.cloneDatabase("127.0.0.1");
5.從指定的機器上復制指定數據庫數據到某個數據庫
db.copyDatabase("mydb","temp","127.0.0.1")
6.修復當前數據庫
db.repairDatabase();
7.查看當前使用的數據庫
db.getName();
db;
8.顯示當前db狀態
db.stats();
9.查看當前db的鏈接機器地址
db.getMongo();
二.Collection聚集集合
1.創建一個聚集集合(table)
db.createCollection(“collName”, {size: 20, capped: 5, max: 100});//創建成功會顯示{“ok”:1}
//判斷集合是否為定容量db.collName.isCapped();
2.得到指定名稱
db.getCollection("account");
3.得到當前db的所有集合
db.getCollectionNames();
4.顯示當前db所有聚集索引的狀態
db.printCollectionStats();
三.用戶相關
1.添加一個用戶
db.addUser("name");
db.addUser("userName", "pwd123", true); //添加用戶、設置密碼、是否只讀
2.數據庫認證 安全模式
db.auth("userName", "123123");
3.顯示當前所有用戶
show users;
4.刪除用戶
db.removeUser("userName");
四.聚合集合查詢
1.查詢所有記錄
db.userInfo.find();
//相當于: select* from userInfo;
默認每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁數據。注意:鍵入it命令不能帶“;”
但是你可以設置每頁顯示數據的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。
2.查詢去掉后的當前聚集集合中的某列的重復數據
db.userInfo.distinct("name");
//會過濾掉name中的相同數據
//相當于:select distict name from userInfo;
3.查詢數值范圍內數據
db.userInfo.find({age: {$gte: 23, $lte: 26}});
//大于等于23,小于等于26
4.查詢name中包含mongo的數據
db.userInfo.find({name: /mongo/});
//相當于%%
[code]select * from userInfo where name like ‘%mongo%';
5.查詢name中以mongo開頭的
db.userInfo.find({name: /^mongo/});
//select * from userInfo where name like ‘mongo%';
6.查詢指定列name age數據
db.userInfo.find({}, {name: 1, age: 1});
//相當于:select name, age from userInfo;
7.查詢前5條數據
db.userInfo.find().limit(5);
//相當于:selecttop 5 * from userInfo;
8.查詢10條以后的數據
db.userInfo.find().skip(10);
//相當于:select * from userInfo where id not in (
selecttop 10 * from userInfo
);
9.查詢某個結果集的記錄條數
db.userInfo.find({age: {$gte: 20}}).count();
//相當于:select count(*) from userInfo where age >= 20;
10.按照某列進行排序
db.userInfo.find({sex: {$exists: true}}).count();
//相當于:select count(sex) from userInfo;
這一段里是比較重要的索引部分
五.索引 (index)
1.創建
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});
2.查詢當前聚集集合所有索引
db.userInfo.getIndexes();
3.查看總索引記錄大小
db.userInfo.totalIndexSize();
4.讀取當前集合的所有index信息
db.users.reIndex();
5.刪除指定索引
db.users.dropIndex("name_1");
6.刪除所有索引
db.users.dropIndexes();
索引這塊可能詳細說,有很所需要注意的地方,要不斷翻閱琢磨!
這一段里是比較重要的索引部分
六. 修改 添加 刪除集合數據
1.添加(save)
db.users.save({name: ‘zhangsan', age: 25, sex: true});
//添加的數據的數據列,沒有固定,根據添加的數據為準
2.修改(update)
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.刪除(remove)
db.users.remove({age: 132});
4.查詢修改刪除
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
});
七.查看基本信息
查看聚集集合基本信息
1、查看幫助 db.yourColl.help();
2、查詢當前集合的數據條數 db.yourColl.count();
3、查看數據空間大小 db.userInfo.dataSize();
4、得到當前聚集集合所在的db db.userInfo.getDB();
5、得到當前聚集的狀態 db.userInfo.stats();
6、得到聚集集合總大小 db.userInfo.totalSize();
7、聚集集合儲存空間大小 db.userInfo.storageSize();
8、Shard版本信息 db.userInfo.getShardVersion();
9、聚集集合重命名 db.userInfo.renameCollection("users"); //將userInfo重命名為users
10、刪除當前聚集集合 db.userInfo.drop();
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