返回
Featured image of post MongoDB - 深入Delete

MongoDB - 深入Delete

MongoDB - 想刪?勇敢刪

基本使用

deleteOne - 單筆刪除(只回傳筆數)

const collection: Collection<User> = db.collection<User>("user");
const query = {
  name: 'Fred'
}
await collection.deleteOne(query);

findOneAndDelete - 查詢而刪除(會回傳查的資料)

const collection: Collection<User> = db.collection<User>("user");
const query = {
  name: 'Fred'
}
await collection.findOneAndDelete(query);

deleteMany - 多筆刪除(只回傳筆數)

const collection: Collection<User> = db.collection<User>("user");
const query = {
  name: /F/  // 正則過濾
}
await collection.deleteMany(query);
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus