30

Can someone please show me, if there is a better way to remove one document from MongoDB using the Official C# Driver than what I have below-

var query = Query.EQ("_id", a.Id);
database.GetCollection<Animal>("Animal").Remove(query);

This code works, but seems too much work to me. The "Save" command for example- takes an instance and updates it. I want something like- Remove(item).

Remarks: I'm trying to use the official driver of C# rather than NoRM or Samus which seems out of date.

Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
Travis Laborde
  • 1,323
  • 1
  • 14
  • 22

6 Answers6

35

That's the way you do it. I'm sure you know this, but if you want to put it on one line you could combine it so you don't need to define a query variable:

collection.Remove(Query.EQ("_id", a.Id));
Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • I'm trying to remove by a field other than _id, and I tried Query.EQ("FieldName", value) but it didn't work. How should I do that? Actually, your code gives the same error either : "only classes can be mapped currently." – Burak Karakuş Mar 04 '15 at 13:46
  • I just realized that I used a linq query, not the Query.EQ() above, and I guess that was the problem because it worked when I changed it to Query.EQ() query. Thanks for your help. – Burak Karakuş Mar 04 '15 at 17:56
  • 5
    This is not longer the answer, MongoDb Driver has changed significantly, this is no the correct answer: collection.DeleteOne(a => a.Id==id); – Brian Ogden Sep 12 '17 at 23:14
25

The Simplest Way

Remove a document from a collection for C# MongoDB Driver (v2.0 or later)-

collection.DeleteOne(a => a.Id==id);

Or-

await collection.DeleteOneAsync(a => a.Id==id);
Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
23

If the [id] is string, you must use ObjectId instance explicitly.

var query = Query.EQ("_id", ObjectId.Parse(id));
Ostati
  • 4,623
  • 3
  • 44
  • 48
2

My ASP.NET Core MVC controller's action accepts Id as a string parameter. Then I parse it and use the result in the DeleteOne() statement:

[HttpPost]
public IActionResult Delete(string id)
{
    ObjectId objectId = ObjectId.Parse(id);
    DbContext.Users.DeleteOne(x => x.Id == objectId);
    return null;
}
Aleksei Mialkin
  • 2,257
  • 1
  • 28
  • 26
1
var filter = Builders<BsonDocument>.Filter.Eq("_id",ObjectId.Parse(id));
var x = data.DeleteOne(filter);

I am using this with the current version ( in 2019 ), and it works.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Eugen
  • 21
  • 4
0

If you only want to delete a docuement from a collection :

public async Task<bool> Delete(string id)
{
    FilterDefinition<YourModel> filter = Builders<YourModel>.Filter.Eq(p => p.Id, id);

    var deleteResult = await _context.YourModelCollection.DeleteOneAsync(filter);

    return deleteResult.IsAcknowledged && deleteResult.DeletedCount > 0;
}

And if you want to delete all the docuements :

public async Task<bool> DeleteAll()
{
    FilterDefinition<YourModel> filter = Builders<YourModel>.Filter.Empty;

    var deleteResult = await _context.YourModelCollection.DeleteManyAsync(filter);

    return deleteResult.IsAcknowledged && deleteResult.DeletedCount > 0;
}
Kaveh Naseri
  • 1,102
  • 2
  • 15
  • 24