To delete a single document, create a filter definition that matches the document you want to remove and call the DeleteOne method on a IMongoCollection<T> reference.
IMongoCollection<T> .DeleteOne(<filter>)
The samples filter a User document by its Id and removes it from the collection.
DeleteDocuments.cs
// get a collection referencevar personsCollection = usersDatabase .GetCollection<User>(Constants.UsersCollection);// find a person using an equality filter on its idvar filter =Builders<User>.Filter.Eq(person =>person.Id,appPerson.Id);// delete the personvar personDeleteResult =awaitpersonsCollection.DeleteOneAsync(filter);if (personDeleteResult.DeletedCount==1){Utils.Log($"Document {appPerson.Id} deleted");}
// get a collection referencevar bsonPersonCollection = usersDatabase .GetCollection<BsonDocument>(Constants.UsersCollection);// find a person using a greater than filter on its salary fieldvar bsonSingleFilter =Builders<BsonDocument>.Filter.Gt("salary",2000);// delete the first person that fulfills the filter criteriavar bsonPersonDeleteResult =await bsonPersonCollection .DeleteOneAsync(bsonSingleFilter);
// delete a single documentdb.users.deleteOne({ _id :ObjectId("5e5ff25170dc588dd0870073")})----------------------// sample result{"acknowledged" : true,"deletedCount" : 1}
When your filter criteria matches more the one document, the first document that matches the filter will be removed
Use a field that is unique across a single collection to be more precise
Delete the first document in the collection
To delete the first document in the collection, simply use an empty filter definition.
DeleteDocuments.cs
// get a collection referencevar personsCollection = usersDatabase .GetCollection<User>(Constants.UsersCollection);// create an empty filter definitionvar emptyFilter =Builders<User>.Filter.Empty;// delete the first document in the collectionvar firstPersonDeleteResult =await personsCollection .DeleteOneAsync(emptyFilter);
// get a collection referencevar bsonPersonCollection = usersDatabase .GetCollection<BsonDocument>(Constants.UsersCollection);// create an empty filtervar bsonEmptyFilter =Builders<BsonDocument>.Filter.Empty;// delete the first document in the collectionvar bsonFirstPersonDeleteResult =await bsonPersonCollection .DeleteOneAsync(bsonEmptyFilter);
// delete the first document in the collectiondb.users.deleteOne({})
To remove more that one documents at the same time, create a filter definition to match the documents you wish to delete and use the DeleteMany method on an IMongoCollection<T>.
Syntax: IMongoCollection<T>.DeleteMany(<filter>)
IMongoCollection<T> .DeleteMany(<filter>)
The following example shows how to delete user documents based on the salary field .
DeleteDocuments.cs
var personsCollection = usersDatabase .GetCollection<User>(Constants.UsersCollection);// create a filtervar salaryFilter =Builders<User>.Filter .And(Builders<User>.Filter.Gt(person =>person.Salary,1200),Builders<User>.Filter.Lt(person =>person.Salary,3500) );// for demonstration only - just to validate the total documents removedvar totalPersons =await personsCollection .Find(salaryFilter).CountDocumentsAsync();// delete the documentsvar personsDeleteResult =awaitpersonsCollection.DeleteManyAsync(salaryFilter);if (personsDeleteResult.DeletedCount.Equals(totalPersons)){Utils.Log($"{totalPersons} users deleted");}
var bsonPersonCollection = usersDatabase .GetCollection<BsonDocument>(Constants.UsersCollection);// find all users with salary > 2000var bsonSingleFilter =Builders<BsonDocument>.Filter.Gt("salary",2000);// delete the matched documentsvar bsonPersonsDeleteResult =await bsonPersonCollection .DeleteManyAsync(bsonSingleFilter);
To delete all documents in a collection, you can use the DeleteMany method with an empty filter. If you want though to clear the entire collection, it's faster to just drop it.