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.
var filter = Builders<Restaurant>.Filter .Eq(r => r.Borough, "Brooklyn"); return await _restaurantsCollection.DeleteManyAsync(filter);
For a fully runnable example of the DeleteManyAsync()
operation, see the DeleteManyAsync code sample.
var filter = Builders<Restaurant>.Filter .Eq(r => r.Borough, "Brooklyn"); 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.