mongoDB增刪查改

一葛家、 數(shù)據(jù)庫使用
開啟 mongodb 服務:要管理數(shù)據(jù)庫沟蔑,必須先開啟服務,開啟服務使用 mongod --dbpath c:\mongodb


image.png
管理 mongodb 數(shù)據(jù)庫:mongo    (一定要在新的 cmd 中輸入)
image.png

清屏:

  cls   

查看所有數(shù)據(jù)庫列表

  show dbs  

二、 創(chuàng)建數(shù)據(jù)庫

image.png

使用數(shù)據(jù)庫遗遵、創(chuàng)建數(shù)據(jù)庫

  use student

如果真的想把這個數(shù)據(jù)庫創(chuàng)建成功,逸嘀。

數(shù)據(jù)庫中不能直接插入數(shù)據(jù)车要,只能往集合(collections)中插入數(shù)據(jù)。不需要專門創(chuàng)建集合崭倘,只需要寫點語法插入數(shù)據(jù)就會創(chuàng)建集合:

  db.student.insert({“name”:”xiaoming”});

db.student 系統(tǒng)發(fā)現(xiàn) student 是一個陌生的集合名字翼岁,所以就自動創(chuàng)建了集合

顯示當前的數(shù)據(jù)集合(mysql 中叫表)

  show collections

刪除數(shù)據(jù)庫司光,刪除當前所在的數(shù)據(jù)庫

  db.dropDatabase();

刪除集合琅坡,刪除指定的集合刪除表

刪除集合 db.COLLECTION_NAME.drop()
db.user.drop()

三、 插入(增加)數(shù)據(jù)

插入數(shù)據(jù)残家,隨著數(shù)據(jù)的插入榆俺,數(shù)據(jù)庫創(chuàng)建成功了,集合也創(chuàng)建成功了坞淮。

image.png

四茴晋、 查找數(shù)據(jù)

1、查詢所有記錄

db.userInfo.find();

*相當于:*select* from userInfo;

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

db.userInfo.distinct("name");

*會過濾掉* name *中的相同數(shù)據(jù)*

*相當于:*select distict name from userInfo;

3诺擅、查詢 age = 22 的記錄

db.userInfo.find({"age": 22});
*相當于: select * from userInfo where age = 22;

4、查詢 age > 22 的記錄

db.userInfo.find({age: {$gt: 22}});
*相當于:*select * from userInfo where age >22;

5 啡直、 查 詢 age < 22 的 記 錄

db.userInfo.find({age: {$lt: 22}});
*相當于:*select * from userInfo where age <22;

6烁涌、查詢** age >= 25 的記錄

db.userInfo.find({age: {$gte: 25}});

相當于:*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/});

//*相當于*%%

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

*相當于:*select name, age from userInfo;

*當然* name *也可以用* true *或* false,*當用* ture *的情況下河* name:1 *效果一樣,如果用* false *就**是排除* name*掂榔,顯示* name *以外的列信息继效。*

12 、查詢指定列 name 装获、 age 數(shù)據(jù) , age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

*相當于:*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});

*相當于:*select * from userInfo where name = *‘*zhangsan*’* and age = *‘*22*’*;

15穴豫、查詢前 5 條數(shù)據(jù)

相當于:selecttop 5 * from userInfo;

16凡简、查詢 10 條以后的數(shù)據(jù)

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

17逼友、查詢在 5-10 之間的數(shù)據(jù)

db.userInfo.find().limit(10).skip(5);
可用于分頁,limit 是 pageSize秤涩,skip 是第幾頁*pageSize

18帜乞、or 與 查詢

db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相當于:select * from userInfo where age = 22 or age = 25;

19、findOne 查詢第一條數(shù)據(jù)

db.userInfo.findOne();
相當于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);

20筐眷、查詢某個結果集的記錄條數(shù) 統(tǒng)計數(shù)量

db.userInfo.find({age: {$gte: 25}}).count();
相當于:select count(*) from userInfo where age >= 20;
如果要返回限制之后的記錄數(shù)量黎烈,要使用 count(true)或者 count(非 0) 
db.users.find().skip(10).limit(5).count(true);

四、修改數(shù)據(jù)

修改里面還有查詢條件匀谣。你要該誰照棋,要告訴 mongo。
查找名字叫做小明的武翎,把年齡更改為 16 歲:

1 db.student.update({"name":"小明"},{$set:{"age":16}});

查找數(shù)學成績是 70烈炭,把年齡更改為 33 歲:

1 db.student.update({"score.shuxue":70},{$set:{"age":33}});

更改所有匹配項目:"
By default, the update() method updates a single document. To update multiple documents, use
the multi option in the update() method.

1 db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});

完整替換,不出現(xiàn)$set 關鍵字了: 注意

1 db.student.update({"name":"小明"},{"name":"大明","age":16});
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’;

五宝恶、 刪除數(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 } )
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末符隙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子垫毙,更是在濱河造成了極大的恐慌膏执,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件露久,死亡現(xiàn)場離奇詭異,居然都是意外死亡欺栗,警方通過查閱死者的電腦和手機毫痕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來迟几,“玉大人消请,你說我怎么就攤上這事±嗳” “怎么了臊泰?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蚜枢。 經(jīng)常有香客問我缸逃,道長,這世上最難降的妖魔是什么厂抽? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任需频,我火速辦了婚禮,結果婚禮上筷凤,老公的妹妹穿的比我還像新娘昭殉。我一直安慰自己苞七,他們只是感情好,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布挪丢。 她就那樣靜靜地躺著蹂风,像睡著了一般。 火紅的嫁衣襯著肌膚如雪乾蓬。 梳的紋絲不亂的頭發(fā)上惠啄,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音巢块,去河邊找鬼礁阁。 笑死,一個胖子當著我的面吹牛族奢,可吹牛的內(nèi)容都是我干的姥闭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼越走,長吁一口氣:“原來是場噩夢啊……” “哼棚品!你這毒婦竟也來了?” 一聲冷哼從身側響起廊敌,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤铜跑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后骡澈,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锅纺,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鹿驼。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡摔笤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布驱入,位于F島的核電站,受9級特大地震影響氯析,放射性物質(zhì)發(fā)生泄漏亏较。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一掩缓、第九天 我趴在偏房一處隱蔽的房頂上張望宴杀。 院中可真熱鬧,春花似錦拾因、人聲如沸旺罢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扁达。三九已至正卧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間跪解,已是汗流浹背炉旷。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留叉讥,地道東北人窘行。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像图仓,于是被迫代替她去往敵國和親罐盔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,457評論 0 13
  • 只要是數(shù)據(jù)庫那么就絕對離不開最為核心的功能: C U R D ,所以在Mongo 里面對于數(shù)據(jù)的操作也是有支持的,...
    Mr_米飯閱讀 327評論 2 1
  • 簡介 MongoDB 是一個基于分布式文件存儲的NoSQL數(shù)據(jù)庫 由C++語言編寫救崔,運行穩(wěn)定惶看,性能高 旨在為 WE...
    大熊_7d48閱讀 37,116評論 1 9
  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內(nèi)容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 5,746評論 0 10
  • 昨天從張家界坐火車回來快十二點了六孵,太累了 我倒在床上就睡了纬黎,今天早上起來想記錄一下昨天在火車上的所見所聞,我們從武...
    洛超閱讀 561評論 0 0