Delete Many Documents
You can delete more than one document using the DeleteMany()
synchronous
method or the DeleteManyAsync()
asynchronous method on a collection object.
Example
The following code deletes all documents in the restaurants
collection whose
borough
field value equals the word "Brooklyn".
Select the Asynchronous or Synchronous tab to see the corresponding code.
// Creates a filter for all documents that have a // "borough" value of "Brooklyn" var filter = Builders<Restaurant>.Filter .Eq(r => r.Borough, "Brooklyn"); // Asynchronously deletes all documents that match the filter return await _restaurantsCollection.DeleteManyAsync(filter);
For a fully runnable example of the DeleteManyAsync()
operation, see the
DeleteManyAsync code sample.
// Creates a filter for all documents that have a // "borough" value of "Brooklyn" var filter = Builders<Restaurant>.Filter .Eq(r => r.Borough, "Brooklyn"); // Deletes all documents that match the filter return _restaurantsCollection.DeleteMany(filter);
For a fully runnable example of the DeleteMany()
operation, see the
DeleteMany code sample.
Expected Result
Running either of the preceding full examples prints the following results:
Deleting documents... Deleted documents: 6086 Resetting sample data...done.
Additional Information
To learn more about deleting documents, see the Delete Documents guide.
To learn more about using builders, see Operations with Builders.