新聞中心

成都創(chuàng)新互聯(lián)公司是一家專注于成都網站設計、網站制作與策劃設計,龍華網站建設哪家好?成都創(chuàng)新互聯(lián)公司做網站,專注于網站建設十載,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:龍華等地區(qū)。龍華做網站價格咨詢:028-86922220
db.collection_name.remove(
{
justOne:
writeConcern:
}
)
參數說明:
- query:可選參數,定義要刪除文檔的條件;
- justOne:可選參數,如果設為 true 或 1,則只刪除一個文檔,如果不設置該參數,或使用默認值 false,則刪除所有匹配條件的文檔;
- writeConcern:可選參數,定義拋出異常的級別。
【示例】首先我們先向集合中插入以下數據:
> db.course.insert([
... {
... title: 'MongoDB教程',
... author: '編程幫',
... url: 'http://www.biancheng.com/mongodb/index.html'
... },{
... title: 'HTML教程',
... author: '編程幫',
... url: 'http://www.biancheng.com/html/index.html'
... },{
... title: 'C#教程',
... author: '編程幫',
... url: 'http://www.biancheng.com/csharp/index.html'
... }
... ])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
然后使用 remove() 方法刪除集合中 title 為“MongoDB教程”的文檔。
> db.course.remove({'title':'MongoDB教程'})
WriteResult({ "nRemoved" : 1 })
這是我們再使用 find() 方法來查看集合中的文檔就會發(fā)現,title 為“MongoDB教程”的文檔已經不在了。
> db.course.find().pretty()
{
"_id" : ObjectId("603221bfe492ab9be9450308"),
"title" : "HTML教程",
"author" : "編程幫",
"url" : "http://www.biancheng.com/html/index.html"
}
{
"_id" : ObjectId("603221bfe492ab9be9450309"),
"title" : "C#教程",
"author" : "編程幫",
"url" : "http://www.biancheng.com/csharp/index.html"
}
如果集合中有多個符合條件的文檔,但您只想刪除其中的第一個文檔的話,可以將 remove() 方法中的 justOne 參數設置為 1 或 true,如下所示:
db.course.remove({'title':'MongoDB教程'},{justOne:true})
如果您在使用 remove() 方法時沒有指定具體的條件,那么 MongoDB 將刪除集合中的所有文檔,等效于 SQL 語句中的 truncate 命令,如下所示:
>db.course.remove({})
>db.course.find()
分享題目:MongoDB刪除文檔
網址分享:http://www.fisionsoft.com.cn/article/ccogipe.html


咨詢
建站咨詢
