1. 程式人生 > >MongoDB 資料庫建立刪除、表(集合)建立刪除、資料增刪改查

MongoDB 資料庫建立刪除、表(集合)建立刪除、資料增刪改查

資料庫使用

開啟 mongodb 服務:要管理資料庫,必須先開啟服務,開啟服務使用 mongod --dbpath D:\mongodb
這裡寫圖片描述
管理 mongodb 資料庫:mongo (一定要在新的 cmd 中輸入)
這裡寫圖片描述

清屏

cls

檢視所有資料庫列表

show dbs 

這裡寫圖片描述

建立資料庫

這裡寫圖片描述

使用資料庫、建立資料庫

use student 

這裡寫圖片描述

如果真的想把這個資料庫建立成功,那麼必須插入一個數據。
資料庫中不能直接插入資料,只能往集合(collections)中插入資料。不需要專門建立集合,只 需要寫點語法插入資料就會建立集合:

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

這裡寫圖片描述
db.student 系統發現 student 是一個陌生的集合名字,所以就自動建立了集合。

顯示當前的資料集合(mysql 中叫表)

show collections 

這裡寫圖片描述

刪除資料庫,刪除當前所在的資料庫

db.dropDatabase();

這裡寫圖片描述

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

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

插入(增加)資料

插入資料,隨著資料的插入,資料庫建立成功了,集合也建立成功了。

db.表名.insert({"name"
:"zhangsan"});
student 集合名稱(表)

查詢資料

查詢所有記錄

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

查詢去掉後的當前聚集集合中的某列的重複資料

db.userInfo.distinct("name");  
會過濾掉 name 中的相同資料 相當於:select distict name from userInfo; 

查詢 age = 22 的記錄

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

查詢 age > 22 的記錄

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

查詢 age < 22 的記錄

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

查詢 age >= 25 的記錄

db.userInfo.find({age: {$gte: 25}});  
相當於:select * from userInfo where age >= 25; 

查詢 age <= 25 的記錄

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

查詢 age >= 23 並且 age <= 26

注意書寫格式

db.userInfo.find({age: {$gte: 23, $lte: 26}}); 

查詢 name 中包含 mongo 的資料

模糊查詢用於搜尋

db.userInfo.find({name: /mongo/}); 
//相當於%% 
select * from userInfo where name like '%mongo%'; 

查詢 name 中以 mongo 開頭的

db.userInfo.find({name: /^mongo/});  
select * from userInfo where name like 'mongo%'; 

查詢指定列 name、age 資料

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

當然 name 也可以用 true 或 false,當用 ture 的情況下河 name:1 效果一樣,如果用 false 就 是排除 name,顯示 name 以外的列資訊

查詢指定列 name、age 資料, age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});  
相當於:select name, age from userInfo where age >25; 

按照年齡排序 1 升序 -1 降序

升序:db.userInfo.find().sort({age: 1});  
降序:db.userInfo.find().sort({age: -1}); 

查詢 name = zhangsan, age = 22 的資料

db.userInfo.find({name: 'zhangsan', age: 22});  
相當於:select * from userInfo where name = 'zhangsan' and age = '22'; 

查詢前 5 條資料

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

查詢 10 條以後的資料

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

查詢在 5-10 之間的資料

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

or 與 查詢

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

findOne 查詢第一條資料

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

查詢某個結果集的記錄條數 統計數量

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

修改資料

update() 方法用於更新已存在的文件

update() 方法用於更新已存在的文件。語法格式如下:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)
  • query : update的查詢條件,類似sql update查詢內where後面的。
  • update : update的物件和一些更新的操作符(如,inc…)等,也可以理解為sql update查詢內set後面的
  • upsert : 可選,這個引數的意思是,如果不存在update的記錄,是否插入objNew,true為插入,預設是false,不插入。
  • multi : 可選,mongodb 預設是false,只更新找到的第一條記錄,如果這個引數為true,就把按條件查出來多條記錄全部更新。
  • writeConcern :可選,丟擲異常的級別。

修改裡面還有查詢條件。你要該誰,要告訴 mongo。 查詢名字叫做小明的,把年齡更改為 16 歲:

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

查詢數學成績是 70,把年齡更改為 33 歲:

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

更改所有匹配專案:
以上語句只會修改第一條發現的文件,如果你要修改多條相同的文件,則需要設定 multi 引數為 true。
multi : 可選,mongodb 預設是false,只更新找到的第一條記錄,如果這個引數為true,就把按條件查出來多條記錄全部更新。

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

完整替換,不出現$set 關鍵字了: 注意

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'

只更新第一條記錄:

db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );

全部更新:

db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );

只新增第一條:

db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

全部新增加進去:

db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

全部更新:

db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

只更新第一條記錄:

db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );

在3.2版本開始,MongoDB提供以下更新集合文件的方法:

db.collection.updateOne() 向指定集合更新單個文件
db.collection.updateMany() 向指定集合更新多個文件

更新單個文件

db.test_collection.updateOne({"name":"abc"},{$set:{"age":"28"}})

更新多個文件

db.test_collection.updateMany({"age":{$gt:"10"}},{$set:{"status":"xyz"}})

刪除資料

remove() 方法的基本語法格式如下所示:

db.collection.remove(
   <query>,
   <justOne>
)

如果你的 MongoDB 是 2.6 版本以後的,語法格式如下:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

引數說明:

  • query :(可選)刪除的文件的條件。
  • justOne : (可選)如果設為 true 或 1,則只刪除一個文件。
  • writeConcern :(可選)丟擲異常的級別。
db.collectionsNames.remove( { "borough": "Manhattan" } ) 
db.users.remove({age: 132}); 
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )