一玛追、連接數(shù)據(jù)庫(kù)
查看所有數(shù)據(jù)庫(kù)列表
mongo
二税课、創(chuàng)建、查看痊剖、刪除數(shù)據(jù)庫(kù)
1韩玩、使用數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)庫(kù)
use testDbs
如果真的想把這個(gè)數(shù)據(jù)庫(kù)創(chuàng)建成功陆馁,那么必須插入一個(gè)數(shù)據(jù)找颓。mongo數(shù)據(jù)庫(kù)中不能直接插入數(shù)據(jù),只能往集合(collections)中插入數(shù)據(jù)氮惯。
db.user.insert({“name”:”xiaoming”});
此時(shí)在數(shù)據(jù)庫(kù)testDbs中新增了一個(gè)表(集合)user叮雳,這個(gè)表中有一條數(shù)據(jù){“name”:”xiaoming”}
2想暗、查看數(shù)據(jù)庫(kù)
show dbs
3妇汗、顯示當(dāng)前的數(shù)據(jù)集合帘不,(mysql 中叫表)
use testDbs
在數(shù)據(jù)庫(kù)testDbs中查看collections
show collections
4、刪除集合杨箭,刪除指定的集合 刪除表
刪除集合 db.COLLECTION_NAME.drop()
db.user.drop()
5寞焙、刪除數(shù)據(jù)庫(kù),刪除當(dāng)前所在的數(shù)據(jù)庫(kù)
db.dropDatabase();
三互婿、 插入(增加)數(shù)據(jù)
插入數(shù)據(jù)捣郊,隨著數(shù)據(jù)的插入,數(shù)據(jù)庫(kù)創(chuàng)建成功了慈参,集合也創(chuàng)建成功了呛牲。
db.表名.insert({"name":"zhangsan","age":20});
四驮配、 查找數(shù)據(jù)
1娘扩、查詢(xún)所有記錄
db.user.find();
2、查詢(xún)?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù)
db.user.distinct("name");
會(huì)過(guò)濾掉 name 中的相同數(shù)據(jù)
3壮锻、查詢(xún) age = 22 的記錄
db.user.find({"age": 22});
4琐旁、查詢(xún) age > 22 的記錄
db.user.find({age: {$gt: 22}});
5、查詢(xún) age < 22 的記錄
db.user.find({age: {$lt: 22}});
6猜绣、查詢(xún) age >= 25 的記錄
db.user.find({age: {$gte: 25}});
7灰殴、查詢(xún) age <= 25 的記錄
db.user.find({age: {$lte: 25}});
8、查詢(xún) age >= 23 并且 age <= 26 注意書(shū)寫(xiě)格式
db.user.find({age: {$gte: 23, $lte: 26}});
9掰邢、查詢(xún) name 中包含 mongo 的數(shù)據(jù) 模糊查詢(xún)用于搜索
db.user.find({name: /mongo/});
10牺陶、查詢(xún) name 中以 mongo 開(kāi)頭的
db.user.find({name: /^mongo/});
11、查詢(xún)指定列 name辣之、age 數(shù)據(jù)
db.user.find({}, {name: 1, age: 1});
當(dāng)然 name 也可以用 true 或 false,當(dāng)用 ture 的情況下河 name:1 效果一樣义图,如果用 false 就 是排除 name,顯示 name 以外的列信息召烂。
12碱工、查詢(xún)指定列 name、age 數(shù)據(jù), age > 25
db.user.find({age: {$gt: 25}}, {name: 1, age: 1});
13奏夫、按照年齡排序 1 升序怕篷,-1 降序
db.user.find().sort({age: 1});
db.user.find().sort({age: -1});
14、查詢(xún) name = zhangsan, age = 22 的數(shù)據(jù)
db.user.find({name: 'zhangsan', age: 22});
15酗昼、查詢(xún)前 5 條數(shù)據(jù)
db.user.find().limit(5);
16廊谓、查詢(xún) 10 條以后的數(shù)據(jù)
db.user.find().skip(10);
翻頁(yè)查詢(xún)
一頁(yè)數(shù)據(jù)的數(shù)量大小:pageSize麻削,跳轉(zhuǎn)到第3頁(yè)
db.user.find().limit(pageSize).skip((3-1) * pageSize);
17蒸痹、查詢(xún)?cè)?5-10 之間的數(shù)據(jù)
db.user.find().limit(10).skip(5);
18春弥、or 與 查詢(xún)
db.user.find({$or: [{age: 22}, {age: 25}]});
19、findOne 查詢(xún)第一條數(shù)據(jù)
db.user.findOne();
20叠荠、查詢(xún)某個(gè)結(jié)果集的記錄條數(shù) 統(tǒng)計(jì)數(shù)量
db.user.find({age: {$gte: 25}}).count();
五匿沛、通過(guò)創(chuàng)建索引進(jìn)行查詢(xún)優(yōu)化
創(chuàng)建索引的命令
db.user.ensureIndex({"userame":1})
獲取當(dāng)前集合的索引
db.user.getIndexes()
刪除索引的命令
db.user.dropIndex({"username":1})
數(shù)字 1 表示 username 鍵的索引按升序存儲(chǔ),-1 表示 age 鍵的索引按照降序方式存儲(chǔ)榛鼎。
復(fù)合索引
db.user.ensureIndex({"username":1, "age":-1})
該索引被創(chuàng)建后逃呼,基于 username 和 age 的查詢(xún)將會(huì)用到該索引,或者是基于 username 的查詢(xún)也會(huì)用到該索引者娱,但是只是基于 age 的查詢(xún)將不會(huì)用到該復(fù)合索引抡笼。因此可以說(shuō), 如果想用到復(fù)合索引黄鳍,必須在查詢(xún)條件中包含復(fù)合索引中的前 N 個(gè)索引列推姻。
唯一索引
db.user.ensureIndex({"userid":1},{"unique":true})
如果再次插入 userid 重復(fù)的文檔時(shí),MongoDB 將報(bào)錯(cuò)框沟,以提示插入重復(fù)鍵藏古,
四、修改數(shù)據(jù)
查找名字叫做小明的街望,把年齡更改為 16 歲:
db.student.update({"name":"小明"},{$set:{"age":16}});
查找數(shù)學(xué)成績(jī)是 70校翔,把年齡更改為 33 歲:
db.student.update({"score.shuxue":70},{$set:{"age":33}});
更改所有匹配項(xiàng)目:
db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});
不出現(xiàn)$set 關(guān)鍵字就變成了完整替換:
db.student.update({"name":"小明"},{"name":"大明","age":16});
五、 刪除數(shù)據(jù)
db.collectionsNames.remove( { "borough": "Manhattan" } )
db.users.remove({age: 132});
將會(huì)刪除所有匹配的數(shù)據(jù)
只刪除一條數(shù)據(jù)
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
刪除所有的數(shù)據(jù)
db.restaurants.remove( { } )