先看集合post中文檔信息: ?
>?db.post.find();????
{?"_id"?:?ObjectId("54a51cfd7f46906f81b7adcd"),?"bar"?:?"baz"?}????
{?"_id"?:?0?}????
{?"_id"?:?1?}????
{?"_id"?:?2?}????
{?"_id"?:?5,?"test1"?:?0?}????
{?"_id"?:?4,?"test2"?:?2?}????
{?"_id"?:?3,?"test3"?:?3?}????
{?"_id"?:?6,?"test5"?:?5?}????
{?"_id"?:?7,?"test1"?:?1?}????
{?"_id"?:?8,?"test1"?:?1?}
刪除指定的文檔: ?
>?db.post.remove({"_id":5});????
WriteResult({?"nRemoved"?:?1?})????
>?db.post.find();????
{?"_id"?:?ObjectId("54a51cfd7f46906f81b7adcd"),?"bar"?:?"baz"?}????
{?"_id"?:?0?}????
{?"_id"?:?1?}????
{?"_id"?:?2?}????
{?"_id"?:?4,?"test2"?:?2?}????
{?"_id"?:?3,?"test3"?:?3?}????
{?"_id"?:?6,?"test5"?:?5?}????
{?"_id"?:?7,?"test1"?:?1?}????
{?"_id"?:?8,?"test1"?:?1?}
?
刪除所有{“test1”:1}的文檔: ?
>?db.post.remove({"test1":1});????
WriteResult({?"nRemoved"?:?2?})????
>?db.post.find();????
{?"_id"?:?ObjectId("54a51cfd7f46906f81b7adcd"),?"bar"?:?"baz"?}????
{?"_id"?:?0?}????
{?"_id"?:?1?}????
{?"_id"?:?2?}????
{?"_id"?:?4,?"test2"?:?2?}????
{?"_id"?:?3,?"test3"?:?3?}????
{?"_id"?:?6,?"test5"?:?5?}????
>
刪除整個post集合: ? ?
>?db.post.drop()????
true????
>?show?collections????
system.indexes????
>
轉載于:https://blog.51cto.com/281816327/1598301
總結
以上是生活随笔為你收集整理的【MongoDB学习笔记7】深入MongoDB的删除(remove/drop)操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。