MongoDB刪除文檔

來源:互聯網
上載者:User

標籤:nosql   命令   設定   sql   god   truncate   指定   trunc   writer   

remove()方法

MongoDB中的 remove()方法用於從集合中刪除文檔。 remove()方法接受兩個參數。 一個是刪除條件,第二個是標誌:justOne。
criteria - (可選)符合刪除條件的集合將被刪除。
justOne - (可選)如果設定為true或1,則只刪除一個文檔。

文法

remove()方法的基本文法如下 -

>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)


假設mycol集合具有以下資料。

> db.mycol.find({}, {‘_id‘:1, ‘title‘:1})
{ "_id" : 101, "title" : "MongoDB Guide" }
{ "_id" : 102, "title" : "NoSQL Database" }
{ "_id" : 104, "title" : "Python Quick Guide" }
{ "_id" : 100, "title" : "Update By Save()Method." }



以下樣本將刪除_id為“100”的文檔。

> db.mycol.find({}, {‘_id‘:1, ‘title‘:1})
{ "_id" : 101, "title" : "MongoDB Guide" }
{ "_id" : 102, "title" : "NoSQL Database" }
{ "_id" : 104, "title" : "Python Quick Guide" }
{ "_id" : 100, "title" : "Update By Save()Method." }
>
> db.mycol.remove({‘_id‘:100})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({}, {‘_id‘:1, ‘title‘:1})
{ "_id" : 101, "title" : "MongoDB Guide" }
{ "_id" : 102, "title" : "NoSQL Database" }
{ "_id" : 104, "title" : "Python Quick Guide" }



只刪除一條文檔記錄

如果有多條記錄,並且只想刪除第一條記錄,則在remove()方法中設定justOne參數。

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)


刪除所有文檔記錄

刪除所有文檔記錄,可在remove()方法中設定justOne參數。

如果不指定刪除條件,MongoDB 將刪除集合中的所有文檔。 這相當於SQL的truncate命令。

>db.mycol.remove()
>db.mycol.find()

MongoDB刪除文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.