Saturday, November 16, 2019

Delete record from MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;   
                                    
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void DeleteOne(string id)
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //table name

   var filter = Builders<BsonDocument>.Filter.Eq("_id",  BsonObjectId.Parse(id)); //Need to parse else you will get the error

   var res = collection.Find(filter).ToList();
 
   collection.DeleteOne(filter);
}

No comments:

Post a Comment

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...