MongoDB快速入門筆記(六)之MongoDB刪除文檔操作
MongoDB是一個跨平臺,面向文檔的數(shù)據(jù)庫,提供高性能,高可用性和易于擴展。MongoDB是工作在集合和文檔上一種概念。
文檔是一組鍵值對。文檔具有動態(tài)模式。動態(tài)模式是指,在同一個集合的文件不必具有相同一組集合的文檔字段或結(jié)構(gòu),并且相同的字段可以保持不同類型的數(shù)據(jù)。
db.集合名稱.remove({query}, justOne)
query:過濾條件,可選
justOne:是否只刪除查詢到的第一條數(shù)據(jù),值為true或者1時,只刪除一條數(shù)據(jù),默認(rèn)為false,可選。
準(zhǔn)備數(shù)據(jù):把_id為1和2的age都變成28
> db.student.update({_id:1},{$set:{age:28}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.update({_id:2},{$set:{age:28}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
{ "_id" : 2, "name" : "lisi", "age" : 28 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "java" ] }
1、使用兩個參數(shù):
刪除age=28的第一條數(shù)據(jù)
> db.student.remove({age:28}, true) WriteResult({ "nRemoved" : 1 }) > db.student.find() { "_id" : 2, "name" : "lisi", "age" : 28 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "java" ] }
2、使用一個參數(shù):
刪除age=28的所有數(shù)據(jù)
> db.student.remove({age:28}) WriteResult({ "nRemoved" : 2 }) > db.student.find() { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "java" ] }
3、刪除集合的全部數(shù)據(jù),括號中的“{}”必須有,表示空的過濾條件:
> db.student.remove({}) WriteResult({ "nRemoved" : 4 })
另外使用remove()方法刪除的時候,只是刪除數(shù)據(jù),表還會存在。使用drop()方法會把表也刪除,并且drop()的效率要比remove()效率高很多。
相關(guān)文章
window下mongodb在dos下服務(wù)器啟動及連接
這篇文章主要介紹了window下mongodb在dos下服務(wù)器啟動及連接的相關(guān)資料,需要的朋友可以參考下2017-06-06MongoDB詭異問題之sh.stopBalancer卡住的解決方法
這篇文章主要給大家介紹了關(guān)于MongoDB詭異問題之sh.stopBalancer卡住解決的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03MongoDB排序時內(nèi)存大小限制與創(chuàng)建索引的注意事項詳解
在數(shù)據(jù)量超大的情形下,任何數(shù)據(jù)庫系統(tǒng)在創(chuàng)建索引時都是一個耗時的大工程,下面這篇文章主要給大家介紹了關(guān)于MongoDB排序時內(nèi)存大小限制與創(chuàng)建索引的注意事項的相關(guān)資料,需要的朋友可以參考下2022-05-05MongoDB服務(wù)端JavaScript腳本使用方法
這篇文章主要介紹了MongoDB服務(wù)端JavaScript腳本使用方法,需要的朋友可以參考下2015-10-10