MongoDB實(shí)現(xiàn)創(chuàng)建刪除數(shù)據(jù)庫(kù)、創(chuàng)建刪除表(集合 )、數(shù)據(jù)增刪改查
一、 數(shù)據(jù)庫(kù)使用
開(kāi)啟 mongodb 服務(wù):要管理數(shù)據(jù)庫(kù),必須先開(kāi)啟服務(wù),開(kāi)啟服務(wù)使用
mongod --dbpath c:\mongodb
管理 mongodb 數(shù)據(jù)庫(kù):(一定要在新的 cmd 中輸入)
mongo
清屏:
cls
查看所有數(shù)據(jù)庫(kù)列表
show dbs
二、 創(chuàng)建數(shù)據(jù)庫(kù)
使用數(shù)據(jù)庫(kù)、創(chuàng)建數(shù)據(jù)庫(kù)
use student
如果真的想把這個(gè)數(shù)據(jù)庫(kù)創(chuàng)建成功,那么必須插入一個(gè)數(shù)據(jù)。
數(shù)據(jù)庫(kù)中不能直接插入數(shù)據(jù),只能往集合(collections)中插入數(shù)據(jù)。不需要專門(mén)創(chuàng)建集合,只
需要寫(xiě)點(diǎn)語(yǔ)法插入數(shù)據(jù)就會(huì)創(chuàng)建集合:
插入一條數(shù)據(jù)
db.student.insert({“name”:”xiaoming”});
db.student 系統(tǒng)發(fā)現(xiàn) student 是一個(gè)陌生的集合名字,所以就自動(dòng)創(chuàng)建了集合。
顯示當(dāng)前的數(shù)據(jù)集合(mysql 中叫表)
show collections
刪除數(shù)據(jù)庫(kù),刪除當(dāng)前所在的數(shù)據(jù)庫(kù)
db.dropDatabase();
刪除集合,刪除指定的集合 刪除表
刪除集合
db.COLLECTION_NAME.drop() db.user.drop()
三、插入(增加)數(shù)據(jù)
插入數(shù)據(jù),隨著數(shù)據(jù)的插入,數(shù)據(jù)庫(kù)創(chuàng)建成功了,集合也創(chuàng)建成功了。
db. 表名.insert({"name":"zhangsan"}); student 集合名稱(表)
四、查找數(shù)據(jù)
1 、查詢所有記錄
db.userInfo.find();
相當(dāng)于:select* from userInfo;
2 、查詢?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù)
db.userInfo.distinct("name");
會(huì)過(guò)濾掉 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 注意書(shū)寫(xiě)格式
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 開(kāi)頭的
db.userInfo.find({name: /^mongo/});
相當(dāng)于: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 、按照年齡排序 1 升序 -1 降序
升序: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 ( select top 10 * from userInfo );
五、刪除數(shù)據(jù)
db.collectionsNames.remove( { "borough": "Manhattan" } ) db.users.remove({age: 132}); By default, the remove() method removes all documents that match the remove condition. Use the justOne option to limit the remove operation to only one of the matching documents. db.restaurants.remove( { "borough": "Queens" }, { justOne: true }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。