mongo小試

MongoDB的一些小結

強大 靈活 可擴展

1.擴展了關系型數(shù)據(jù)庫的輔助索引 范圍查詢(range query)和排序。
內(nèi)置的對MapReduce式聚合的支持,以及對地理空間索引的支持。

2.面向文檔的數(shù)據(jù)庫,思路首先就是將原來的“行”(row)的概念換成更加靈活的“文檔”(documents)。面向文檔的方式可以將文檔或者數(shù)組內(nèi)嵌進來,所以用一條記錄就可以表示非常復雜的層次關系。這對于面向對象語言的開發(fā)者來說,是非常自然的事情。

我們先來看看一些常見的操作命令
首先進入命令行之后默認的是test數(shù)據(jù)庫,一般而言我們首先需要切換到我們需要的數(shù)據(jù)庫,比如這里舉例一下 guazigz
use guazigz
再來詳細學習

一.數(shù)據(jù)庫常用命令

1.help查看命令提示

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

這里的youColl換成你數(shù)據(jù)庫里對應的集合(Collection)即可

2.查詢數(shù)據(jù)庫

show dbs;

3.刪除當前使用數(shù)據(jù)庫

db.dropDatabase();

4.從指定的主機上克隆數(shù)據(jù)庫

db.cloneDatabase("127.0.0.1");

5.從指定的機器上復制指定數(shù)據(jù)庫數(shù)據(jù)到某個數(shù)據(jù)庫

db.copyDatabase("mydb","temp","127.0.0.1")

6.修復當前數(shù)據(jù)庫

db.repairDatabase();

7.查看當前使用的數(shù)據(jù)庫

db.getName();
db;

8.顯示當前db狀態(tài)

db.stats();

9.查看當前db的鏈接機器地址

db.getMongo();

二.Collection聚集集合
1.創(chuàng)建一個聚集集合(table)

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

2.得到指定名稱

db.getCollection("account");

3.得到當前db的所有集合

db.getCollectionNames();

4.顯示當前db所有聚集索引的狀態(tài)

db.printCollectionStats();

三.用戶相關
1.添加一個用戶

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

2.數(shù)據(jù)庫認證 安全模式

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

3.顯示當前所有用戶

show users;

4.刪除用戶

db.removeUser("userName");

四.聚合集合查詢

1.查詢所有記錄

db.userInfo.find();
//相當于: select* from userInfo;

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

2.查詢?nèi)サ艉蟮漠斍熬奂现械哪沉械闹貜蛿?shù)據(jù)

db.userInfo.distinct("name");
//會過濾掉name中的相同數(shù)據(jù)
//相當于:select distict name from userInfo;

3.查詢數(shù)值范圍內(nèi)數(shù)據(jù)

db.userInfo.find({age: {$gte: 23, $lte: 26}});
//大于等于23,小于等于26

4.查詢name中包含mongo的數(shù)據(jù)

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數(shù)據(jù)

db.userInfo.find({}, {name: 1, age: 1});
//相當于:select name, age from userInfo;

7.查詢前5條數(shù)據(jù)

db.userInfo.find().limit(5);
//相當于:selecttop 5 * from userInfo;

8.查詢10條以后的數(shù)據(jù)

db.userInfo.find().skip(10);
//相當于:select * from userInfo where id not in (
selecttop 10 * from userInfo
);

9.查詢某個結果集的記錄條數(shù)

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.創(chuàng)建

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();

索引這塊可能詳細說,有很所需要注意的地方,要不斷翻閱琢磨!

這一段里是比較重要的索引部分

六. 修改 添加 刪除集合數(shù)據(jù)

1.添加(save)

db.users.save({name: ‘zhangsan', age: 25, sex: true});
//添加的數(shù)據(jù)的數(shù)據(jù)列,沒有固定,根據(jù)添加的數(shù)據(jù)為準

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、查詢當前集合的數(shù)據(jù)條數(shù) db.yourColl.count();
3、查看數(shù)據(jù)空間大小 db.userInfo.dataSize();
4、得到當前聚集集合所在的db db.userInfo.getDB();
5、得到當前聚集的狀態(tài) 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:顯示數(shù)據(jù)庫列表 
show collections:顯示當前數(shù)據(jù)庫中的集合(類似關系數(shù)據(jù)庫中的表) 
show users:顯示用戶 
use <db name>:切換當前數(shù)據(jù)庫,這和MS-SQL里面的意思一樣 
db.help():顯示數(shù)據(jù)庫操作命令,里面有很多的命令 
db.foo.help():顯示集合操作命令,同樣有很多的命令,foo指的是當前數(shù)據(jù)庫下,一個叫foo的集合,并非真正意義上的命令 
db.foo.find():對于當前數(shù)據(jù)庫中的foo集合進行數(shù)據(jù)查找(由于沒有條件,會列出所有數(shù)據(jù)) 
db.foo.find( { a : 1 } ):對于當前數(shù)據(jù)庫中的foo集合進行查找,條件是數(shù)據(jù)中有一個屬性叫a,且a的值為1
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 一、數(shù)據(jù)庫常用命令1、Help查看命令提示 復制代碼 代碼如下: helpdb.help();db.yourCol...
    字伯約閱讀 439評論 0 0
  • show dbs:顯示數(shù)據(jù)庫列表 show collections:顯示當前數(shù)據(jù)庫中的集合(類似關系數(shù)據(jù)庫中的表)...
    Weiliam閱讀 7,568評論 0 2
  • 成功啟動MongoDB后,再打開一個命令行窗口輸入mongo,就可以進行數(shù)據(jù)庫的一些操作。 輸入help可以看到基...
    你本來就很牛閱讀 28,791評論 0 3
  • 本課重點:學習MongoDB數(shù)據(jù)庫的命令操作。MongoDB是一個NoSQL數(shù)據(jù)庫系統(tǒng):一個數(shù)據(jù)庫可以包含多個集合...
    瘋范兒閱讀 449評論 0 0
  • 數(shù)據(jù)庫排行 mongoDB 非關系型數(shù)據(jù)庫 介紹 MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫。由C++語言編寫...
    依惜在昨天閱讀 336評論 0 0

友情鏈接更多精彩內(nèi)容