.net MVC中實現MongoDB按照 _id 值刪除單條記錄
阿新 • • 發佈:2018-12-24
[HttpPost] public JsonResult DeleteAdminLog(string id = "") { var filter = Builders<BsonDocument>.Filter.Eq("_id", new ObjectId(id)); var collection = DBHelper.GetCollection("AdminLog"); var result = collection.DeleteOne(filter); if (result.DeletedCount > 0) { return Json("刪除成功"); } else { return Json("刪除失敗"); } }
這裡必須用 new ObjectId(id)對傳入的值進行轉換,否則刪除失敗