成功啟動(dòng)MongoDB后骇扇,再打開一個(gè)命令行窗口輸入mongo,就可以進(jìn)行數(shù)據(jù)庫(kù)的一些操作迹冤。
輸入help可以看到基本操作命令:
show dbs:顯示數(shù)據(jù)庫(kù)列表? show collections:顯示當(dāng)前數(shù)據(jù)庫(kù)中的集合(類似關(guān)系數(shù)據(jù)庫(kù)中的表)? show users:顯示用戶
use <db name>:切換當(dāng)前數(shù)據(jù)庫(kù)逾礁,這和MS-SQL里面的意思一樣? db.help():顯示數(shù)據(jù)庫(kù)操作命令霹购,里面有很多的命令? db.foo.help():顯示集合操作命令,同樣有很多的命令溢陪,foo指的是當(dāng)前數(shù)據(jù)庫(kù)下萍虽,一個(gè)叫foo的集合笛辟,并非真正意義上的命令? db.foo.find():對(duì)于當(dāng)前數(shù)據(jù)庫(kù)中的foo集合進(jìn)行數(shù)據(jù)查找(由于沒有條件届榄,會(huì)列出所有數(shù)據(jù))? db.foo.find( { a : 1 } ):對(duì)于當(dāng)前數(shù)據(jù)庫(kù)中的foo集合進(jìn)行查找,條件是數(shù)據(jù)中有一個(gè)屬性叫a赌厅,且a的值為1
MongoDB沒有創(chuàng)建數(shù)據(jù)庫(kù)的命令,但有類似的命令邓馒。
如:如果你想創(chuàng)建一個(gè)“myTest”的數(shù)據(jù)庫(kù)嘶朱,先運(yùn)行use myTest命令,之后就做一些操作(如:db.createCollection('user')),這樣就可以創(chuàng)建一個(gè)名叫“myTest”的數(shù)據(jù)庫(kù)光酣。
數(shù)據(jù)庫(kù)常用命令
1疏遏、Help查看命令提示
help
? db.help();
? db.yourColl.help();
? db.youColl.find().help();
? rs.help();
2、切換/創(chuàng)建數(shù)據(jù)庫(kù)
use yourDB;? 當(dāng)創(chuàng)建一個(gè)集合(table)的時(shí)候會(huì)自動(dòng)創(chuàng)建當(dāng)前數(shù)據(jù)庫(kù)
3救军、查詢所有數(shù)據(jù)庫(kù)
show dbs;
4财异、刪除當(dāng)前使用數(shù)據(jù)庫(kù)
db.dropDatabase();
5、從指定主機(jī)上克隆數(shù)據(jù)庫(kù)
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ù)
db.copyDatabase("mydb", "temp", "127.0.0.1");將本機(jī)的mydb的數(shù)據(jù)復(fù)制到temp數(shù)據(jù)庫(kù)中
7宝当、修復(fù)當(dāng)前數(shù)據(jù)庫(kù)
db.repairDatabase();
8、查看當(dāng)前使用的數(shù)據(jù)庫(kù)
db.getName();
db; db和getName方法是一樣的效果胆萧,都可以查詢當(dāng)前使用的數(shù)據(jù)庫(kù)
9庆揩、顯示當(dāng)前db狀態(tài)
db.stats();
10、當(dāng)前db版本
db.version();
11跌穗、查看當(dāng)前db的鏈接機(jī)器地址
db.getMongo();
Collection聚集集合
1订晌、創(chuàng)建一個(gè)聚集集合(table)
db.createCollection(“collName”, {size: 20, capped: 5, max: 100});
2、得到指定名稱的聚集集合(table)
db.getCollection("account");
3蚌吸、得到當(dāng)前db的所有聚集集合
db.getCollectionNames();
4锈拨、顯示當(dāng)前db所有聚集索引的狀態(tài)
db.printCollectionStats();
用戶相關(guān)
1、添加一個(gè)用戶
db.addUser("name");
db.addUser("userName", "pwd123", true); 添加用戶羹唠、設(shè)置密碼奕枢、是否只讀
2、數(shù)據(jù)庫(kù)認(rèn)證佩微、安全模式
db.auth("userName", "123123");
3缝彬、顯示當(dāng)前所有用戶
show users;
4、刪除用戶
db.removeUser("userName");
其他
1哺眯、查詢之前的錯(cuò)誤信息
db.getPrevError();
2谷浅、清除錯(cuò)誤記錄
db.resetError();
查看聚集集合基本信息
1、查看幫助? db.yourColl.help();
2奶卓、查詢當(dāng)前集合的數(shù)據(jù)條數(shù)? db.yourColl.count();
3一疯、查看數(shù)據(jù)空間大小 db.userInfo.dataSize();
4、得到當(dāng)前聚集集合所在的db db.userInfo.getDB();
5夺姑、得到當(dāng)前聚集的狀態(tài) db.userInfo.stats();
6墩邀、得到聚集集合總大小 db.userInfo.totalSize();
7、聚集集合儲(chǔ)存空間大小 db.userInfo.storageSize();
8盏浙、Shard版本信息? db.userInfo.getShardVersion()
9磕蒲、聚集集合重命名 db.userInfo.renameCollection("users"); 將userInfo重命名為users
10留潦、刪除當(dāng)前聚集集合 db.userInfo.drop();
聚集集合查詢
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ù)
db.userInfo.distinct("name");
會(huì)過濾掉name中的相同數(shù)據(jù)
相當(dāng)于:select distict name from userInfo;
3十偶、查詢age = 22的記錄
db.userInfo.find({"age": 22});
相當(dāng)于: select * from userInfo where age = 22;
4、查詢age > 22的記錄
db.userInfo.find({age: {$gt: 22}});
相當(dāng)于:select * from userInfo where age >22;
5园细、查詢age < 22的記錄
db.userInfo.find({age: {$lt: 22}});
相當(dāng)于:select * from userInfo where age <22;
6惦积、查詢age >= 25的記錄
db.userInfo.find({age: {$gte: 25}});
相當(dāng)于:select * from userInfo where age >= 25;
7、查詢age <= 25的記錄
db.userInfo.find({age: {$lte: 25}});
8猛频、查詢age >= 23 并且 age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});
9狮崩、查詢name中包含 mongo的數(shù)據(jù)
db.userInfo.find({name: /mongo/});
//相當(dāng)于%%
select * from userInfo where name like ‘%mongo%’;
10、查詢name中以mongo開頭的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;
11鹿寻、查詢指定列name睦柴、age數(shù)據(jù)
db.userInfo.find({}, {name: 1, age: 1});
相當(dāng)于:select name, age from userInfo;
當(dāng)然name也可以用true或false,當(dāng)用ture的情況下河name:1效果一樣,如果用false就是排除name毡熏,顯示name以外的列信息坦敌。
12、查詢指定列name痢法、age數(shù)據(jù), age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相當(dāng)于:select name, age from userInfo where age >25;
13狱窘、按照年齡排序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
14、查詢name = zhangsan, age = 22的數(shù)據(jù)
db.userInfo.find({name: 'zhangsan', age: 22});
相當(dāng)于:select * from userInfo where name = ‘zhangsan’ and age = ‘22’;
15财搁、查詢前5條數(shù)據(jù)
db.userInfo.find().limit(5);
相當(dāng)于:selecttop 5 * from userInfo;
16蘸炸、查詢10條以后的數(shù)據(jù)
db.userInfo.find().skip(10);
相當(dāng)于:select * from userInfo where id not in (
selecttop 10 * from userInfo
);
17、查詢?cè)?-10之間的數(shù)據(jù)
db.userInfo.find().limit(10).skip(5);
可用于分頁(yè)妇拯,limit是pageSize幻馁,skip是第幾頁(yè)*pageSize
18洗鸵、or與 查詢
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相當(dāng)于:select * from userInfo where age = 22 or age = 25;
19越锈、查詢第一條數(shù)據(jù)
db.userInfo.findOne();
相當(dāng)于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);
20、查詢某個(gè)結(jié)果集的記錄條數(shù)
db.userInfo.find({age: {$gte: 25}}).count();
相當(dāng)于:select count(*) from userInfo where age >= 20;
21膘滨、按照某列進(jìn)行排序
db.userInfo.find({sex: {$exists: true}}).count();
相當(dāng)于:select count(sex) from userInfo;
索引
1甘凭、創(chuàng)建索引
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});
2、查詢當(dāng)前聚集集合所有索引
db.userInfo.getIndexes();
3火邓、查看總索引記錄大小
db.userInfo.totalIndexSize();
4丹弱、讀取當(dāng)前集合的所有index信息
db.users.reIndex();
5德撬、刪除指定索引
db.users.dropIndex("name_1");
6、刪除所有索引索引
db.users.dropIndexes();
修改躲胳、添加蜓洪、刪除集合數(shù)據(jù)
1、添加
db.users.save({name: ‘zhangsan’, age: 25, sex: true});
添加的數(shù)據(jù)的數(shù)據(jù)列坯苹,沒有固定隆檀,根據(jù)添加的數(shù)據(jù)為準(zhǔn)
2、修改
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’;
3粹湃、刪除
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
});
update 或 remove 其中一個(gè)是必須的參數(shù); 其他參數(shù)可選。
參數(shù)
詳解
默認(rèn)值
query
查詢過濾條件
{}
sort
如果多個(gè)文檔符合查詢過濾條件为鳄,將以該參數(shù)指定的排列方式選擇出排在首位的對(duì)象裳仆,該對(duì)象將被操作
{}
remove
若為true,被選中對(duì)象將在返回前被刪除
N/A
update
一個(gè) 修改器對(duì)象
N/A
new
若為true孤钦,將返回修改后的對(duì)象而不是原始對(duì)象歧斟。在刪除操作中,該參數(shù)被忽略司训。
false
fields
參見Retrieving a Subset of Fields (1.5.0+)
All fields
upsert
創(chuàng)建新對(duì)象若查詢結(jié)果為空构捡。 示例 (1.5.4+)
false
語(yǔ)句塊操作
1、簡(jiǎn)單Hello World
print("Hello World!");
這種寫法調(diào)用了print函數(shù)壳猜,和直接寫入"Hello World!"的效果是一樣的勾徽;
2、將一個(gè)對(duì)象轉(zhuǎn)換成json
tojson(new Object());
tojson(new Object('a'));
3统扳、循環(huán)添加數(shù)據(jù)
> for (var i = 0; i < 30; i++) {
... db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
... };
這樣就循環(huán)添加了30條數(shù)據(jù)喘帚,同樣也可以省略括號(hào)的寫法
> for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
也是可以的,當(dāng)你用db.users.find()查詢的時(shí)候咒钟,顯示多條數(shù)據(jù)而無(wú)法一頁(yè)顯示的情況下吹由,可以用it查看下一頁(yè)的信息;
4朱嘴、find 游標(biāo)查詢
>var cursor = db.users.find();
> while (cursor.hasNext()) {
? ? printjson(cursor.next());
}
這樣就查詢所有的users信息倾鲫,同樣可以這樣寫
var cursor = db.users.find();
while (cursor.hasNext()) { printjson(cursor.next); }
同樣可以省略{}號(hào)
5、forEach迭代循環(huán)
db.users.find().forEach(printjson);
forEach中必須傳遞一個(gè)函數(shù)來(lái)處理每條迭代的數(shù)據(jù)信息
6萍嬉、將find游標(biāo)當(dāng)數(shù)組處理
var cursor = db.users.find();
cursor[4];
取得下標(biāo)索引為4的那條數(shù)據(jù)
既然可以當(dāng)做數(shù)組處理乌昔,那么就可以獲得它的長(zhǎng)度:cursor.length();或者cursor.count();
那樣我們也可以用循環(huán)顯示數(shù)據(jù)
for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);
7、將find游標(biāo)轉(zhuǎn)換成數(shù)組
> var arr = db.users.find().toArray();
> printjson(arr[2]);
用toArray方法將其轉(zhuǎn)換為數(shù)組
8壤追、定制我們自己的查詢結(jié)果
只顯示age <= 28的并且只顯示age這列數(shù)據(jù)
db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);
db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);
排除age的列
db.users.find({age: {$lte: 28}}, {age: false}).forEach(printjson);
9磕道、forEach傳遞函數(shù)顯示信息
db.things.find({x:4}).forEach(function(x) {print(tojson(x));});