MongoDB實現(xiàn)增刪改查
一、增加
insert向數(shù)據(jù)庫中插入集合
插入一條記錄,傳入集合
db..insert() db.students.insert({name:"唐僧",age:60,gender:"M"})
插入多條記錄,傳入數(shù)組
db.students.insert([{name:"豬八戒",age:53,gender:"M"},{name:"沙僧",age:50,gender:"M"}])
默認(rèn)生成時間戳id,確保數(shù)據(jù)唯一,原理是調(diào)用如下語句,可以自行指定id
默認(rèn):
ObjectId()
自行指定:
db.students.insert({_id:"001",name:"白骨精",age:60,gender:"W"})
insertOne向數(shù)據(jù)庫中插入集合
插入一個文檔對象,只能傳入一個文檔,不能傳入數(shù)組
db.students.insertOne({_id:"002",name:"鐵扇公主 ",age:60,gender:"W"})
insertMany向數(shù)據(jù)庫中插入集合
插入多個文檔對象,只能傳入一個數(shù)組,即使只存在一個文檔,不能傳入文檔
db.students.insertMany([{_id:"004",name:"牛魔王",age:60,gender:"M"},{_id:"005",name:"紅孩兒",age:60,gender:"M"}])
其實是對insert的拆分
二、查詢
查詢集合中所有符合條件的文檔
find進行查詢集合所有文檔
db.students.find(); db.students.find({});
find進行條件查詢
{}:表示集合中所有文檔;
{屬性:值} 查詢屬性是指定值的文檔
db.students.find({_id:"002"}); db.students.find({_id:"002",name:"劉德華"}); db.students.find({_id:"002"}).count(); ----查看查詢的文檔的數(shù)量 db.students.find({_id:"002"}).length();
find返回值為數(shù)組,可以通過下標(biāo)獲取對應(yīng)值
findOne進行條件查詢
findOne返回的為Object,可以用返回值.對象
查詢集合中符合條件的第一個文檔,最多一條文檔
db.students.findOne({name:"AideHua"});
三、修改
update(查詢條件,新對象)
db..update(查詢條件,新對象)
如:db.students.update({_id:"002"},{age:18}) 注意:將會用新對象{age:18}替換之前舊對象
修改前
修改后
update(查詢條件,{$set:修改文檔屬性})
db.students.update({_id:"004"},{$set:{age:180}})
修改操作符
set ------修改文檔指定屬性
unset------刪除文檔指定屬性(和值無關(guān))
默認(rèn)只會修改一個
如果想要進行修改多個,指定可選參數(shù)multi
db.students.update({_id:"004"},{$set:{age:880}},{multi:true})
updateMany()
同時修改多個符合條件的文檔
匹配多少個,就會修改多少個的值
db.students.updateMany({_id:"004"},{$set:{age:180}})
updateOne()
修改一個符合條件的文檔
只會修改匹配的第一個
db.students.updateOne({_id:"004"},{$set:{age:180}})
replaceOne()
替換文檔
四、刪除
remove()
刪除符合條件的而所有文檔,默認(rèn)刪除所有,刪除單個需要指定可選參數(shù)justOne為:true
刪除多個,默認(rèn)情況
db.students.remove({name:"Liming"})
刪除單個
db.students.remove({name:"Liming"},true)
必須指定參數(shù),否則報錯,注意和find()區(qū)別
db.students.remove();//報錯
刪除所有文檔,但是集合還是存在的
db.students.remove({});
刪除集合,若是數(shù)據(jù)庫的最后一個人集合,那么數(shù)據(jù)庫也會被刪除
db.students.drop();
deleteOne()
刪除一個
db.students.deleteOne({name:"Liming"})
deleteMany()
刪除多個
db.students.deleteMany({name:"Liming"})
到此這篇關(guān)于MongoDB實現(xiàn)增刪改查的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot集成mongodb的增刪改查的示例代碼
- mongodb數(shù)據(jù)庫實驗之增刪查改
- Java springboot Mongodb增刪改查代碼實例
- express+mongoose實現(xiàn)對mongodb增刪改查操作詳解
- java連接Mongodb實現(xiàn)增刪改查
- MongoDB增刪查改操作示例【基于JavaScript Shell】
- MongoDB簡單操作示例【連接、增刪改查等】
- Node.js對MongoDB進行增刪改查操作的實例代碼
- nodejs操作mongodb的增刪改查功能實例
- Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能示例
- mongodb增刪改查詳解_動力節(jié)點Java學(xué)院整理
相關(guān)文章
Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程
不得不說MongoDB的備份回復(fù)操作對比其他數(shù)據(jù)庫來說真的算得上是簡便的,無論是在Windows的命令行中或者是Linux里的腳本執(zhí)行,這里我們就來看一下Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程2016-06-06CentOS 6.5 x64系統(tǒng)中安裝MongoDB 2.6.0二進制發(fā)行版教程
這篇文章主要介紹了CentOS 6.5 x64系統(tǒng)中安裝MongoDB 2.6.0二進制發(fā)行版教程,本文分為6個步驟完成MongoDB的安裝和啟動,需要的朋友可以參考下2015-01-01MongoDB自動刪除過期數(shù)據(jù)的方法(TTL索引)
這篇文章主要給大家介紹了關(guān)于MongoDB自動刪除過期數(shù)據(jù)(TTL索引)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實例
這篇文章主要介紹了MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實例的相關(guān)資料,需要的朋友可以參考下2016-10-10