MongoDB 移除資料
承上篇 MongoDB 更新資料,延用相同的 db 和 collections。
使用 mongo shell 操作,刪除資料使用 deleteOne() 或 deleteMany():
> db.inventory.find()
{ "_id" : ObjectId("5ea4715cda0c749138d46e52"), "item" : "paper", "qty" : 100 }
{ "_id" : ObjectId("5ea4715cda0c749138d46e53"), "item" : "journal", "quantity" : 25 }
{ "_id" : ObjectId("5ea4715cda0c749138d46e54"), "item" : "planner", "qty" : 75 }
{ "_id" : ObjectId("5ea4715cda0c749138d46e55"), "item" : "postcard", "qty" : 45 }
{ "_id" : ObjectId("5ea6f2ab40e9db6af06a231a"), "item" : "notebook1", "qty" : 50 }
{ "_id" : ObjectId("5ea6f53240e9db6af06a231b"), "item" : "notebook2", "quantity" : 30 }
{ "_id" : ObjectId("5ea6f53240e9db6af06a231c"), "item" : "notebook3", "quantity" : 13 }
> db.inventory.deleteOne({item:/note/})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.inventory.deleteMany({item:/note/})
{ "acknowledged" : true, "deletedCount" : 2 }
沒有留言:
張貼留言