Skip to content

Commit be255ba

Browse files
authored
DOCSP-24537 delete many usageex (#17)
* test * DOCSP-24537 deleteMany usage example * page added to tree * PR comments 1 * fixed code comments for include * PR comments 2 * namespace correction
1 parent 0a24c29 commit be255ba

File tree

5 files changed

+227
-1
lines changed

5 files changed

+227
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
using MongoDB.Bson.Serialization.Conventions;
4+
using MongoDB.Driver;
5+
using static System.Console;
6+
7+
namespace CSharpExamples.UsageExamples;
8+
9+
public class DeleteMany
10+
{
11+
private static IMongoCollection<Restaurant> _restaurantsCollection;
12+
private static string _mongoConnectionString = "<Your MongoDB URI>";
13+
14+
public static void Main(string[] args)
15+
{
16+
Setup();
17+
18+
var docs = _restaurantsCollection.Find(Builders<Restaurant>.Filter
19+
.Regex("name", "Green")).ToList();
20+
21+
// Deleting documents using builders
22+
WriteLine("Deleting documents...");
23+
var result = DeleteMultipleRestaurantsBuilder();
24+
25+
WriteLine($"Deleted documents: {result.DeletedCount}");
26+
27+
Restore(docs);
28+
}
29+
30+
private static DeleteResult DeleteMultipleRestaurantsBuilder()
31+
{
32+
// start-delete-many-builders
33+
var filter = Builders<Restaurant>.Filter
34+
.Regex("name", "Green");
35+
36+
var result = _restaurantsCollection.DeleteMany(filter);
37+
return result;
38+
// end-delete-many-builders
39+
}
40+
41+
private static void Restore(IEnumerable<Restaurant> docs)
42+
{
43+
_restaurantsCollection.InsertMany(docs);
44+
WriteLine("Resetting sample data...done.");
45+
}
46+
47+
private static void Setup()
48+
{
49+
// This allows automapping of the camelCase database fields to our models.
50+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
51+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
52+
53+
// Establish the connection to MongoDB and get the restaurants database
54+
var mongoClient = new MongoClient(_mongoConnectionString);
55+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
56+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
using MongoDB.Bson.Serialization.Conventions;
4+
using MongoDB.Driver;
5+
using static System.Console;
6+
7+
namespace CSharpExamples.UsageExamples;
8+
9+
public class DeleteManyAsync
10+
{
11+
private static IMongoCollection<Restaurant> _restaurantsCollection;
12+
private static string _mongoConnectionString = "<Your MongoDB URI>";
13+
14+
public static void Main(string[] args)
15+
{
16+
Setup();
17+
18+
var docs = _restaurantsCollection.Find(Builders<Restaurant>.Filter
19+
.Regex("name", "Green")).ToList();
20+
21+
// Deleting documents using builders
22+
WriteLine("Deleting documents...");
23+
var result = DeleteMultipleRestaurantsBuilderAsync();
24+
25+
WriteLine($"Deleted documents: {result.Result.DeletedCount}");
26+
27+
Restore(docs);
28+
}
29+
30+
private static async Task<DeleteResult> DeleteMultipleRestaurantsBuilderAsync()
31+
{
32+
// start-delete-many-async
33+
var filter = Builders<Restaurant>.Filter
34+
.Regex("name", "Green");
35+
36+
var result = await _restaurantsCollection.DeleteManyAsync(filter);
37+
return result;
38+
// end-delete-many-async
39+
}
40+
41+
private static void Restore(IEnumerable<Restaurant> docs)
42+
{
43+
_restaurantsCollection.InsertMany(docs);
44+
WriteLine("Resetting sample data...done.");
45+
}
46+
47+
private static void Setup()
48+
{
49+
// This allows automapping of the camelCase database fields to our models.
50+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
51+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
52+
53+
// Establish the connection to MongoDB and get the restaurants database
54+
var mongoClient = new MongoClient(_mongoConnectionString);
55+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
56+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
namespace UsageExamples.DeleteMany;
4+
5+
// start-model
6+
public class Restaurant
7+
{
8+
public ObjectId Id { get; set; }
9+
10+
public string Name { get; set; }
11+
12+
[BsonElement("restaurant_id")]
13+
public string RestaurantId { get; set; }
14+
15+
public string Cuisine { get; set; }
16+
17+
public object Address { get; set; }
18+
19+
public string Borough { get; set; }
20+
21+
public List<object> Grades { get; set; }
22+
}
23+
// end-model

source/usage-examples.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ TODO: Write usage examples page
1212
/usage-examples/findMany
1313
/usage-examples/updateOne
1414
/usage-examples/updateMany
15-
/usage-examples/deleteOne
15+
/usage-examples/deleteOne
16+
/usage-examples/deleteMany
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.. _csharp-delete-many:
2+
3+
=====================
4+
Delete Many Documents
5+
=====================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
You can delete more than one document using the ``DeleteMany()`` method on
16+
a collection object.
17+
18+
Example
19+
-------
20+
21+
These examples use the following ``Restaurant`` class as a model:
22+
23+
.. literalinclude:: ../includes/code-examples/delete-many/Restaurant.cs
24+
:start-after: start-model
25+
:end-before: end-model
26+
:language: csharp
27+
:copyable:
28+
:dedent:
29+
30+
The following code deletes all documents in the ``restaurants`` collection whose
31+
``name`` field value includes the word "Green".
32+
33+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the corresponding
34+
code.
35+
36+
.. tabs::
37+
38+
.. tab:: Asynchronous
39+
:tabid: delete-many-async
40+
41+
.. literalinclude:: ../includes/code-examples/delete-many/DeleteManyAsync.cs
42+
:start-after: start-delete-many-async
43+
:end-before: end-delete-many-async
44+
:language: csharp
45+
:copyable:
46+
:dedent:
47+
48+
For a fully runnable example of the ``DeleteManyAsync()`` operation, see the
49+
`DeleteManyAsync code sample <{+example+}/delete-many/DeleteManyAsync.cs>`__.
50+
51+
.. tab:: Synchronous
52+
:tabid: delete-many-sync
53+
54+
.. literalinclude:: ../includes/code-examples/delete-many/DeleteMany.cs
55+
:start-after: start-delete-many
56+
:end-before: end-delete-many
57+
:language: csharp
58+
:copyable:
59+
:dedent:
60+
61+
For a fully runnable example of the ``DeleteMany()`` operation, see the
62+
`DeleteMany code sample <{+example+}/delete-many/DeleteMany.cs>`__.
63+
64+
Expected Result
65+
~~~~~~~~~~~~~~~
66+
67+
Running either of the preceding full examples prints the following results:
68+
69+
.. code-block:: none
70+
71+
Deleting documents...
72+
Deleted documents: 102
73+
Resetting sample data...done.
74+
75+
More Information
76+
----------------
77+
78+
.. TODO: Add links to references once pages are done
79+
80+
.. TODO: To learn more about builders, see <builders page>.
81+
82+
API Documentation
83+
-----------------
84+
85+
* `DeleteMany() <{+api-root+}/Overload_MongoDB_Driver_IMongoCollectionExtensions_DeleteMany.htm>`__
86+
* `DeleteManyAsync() <{+api-root+}/Overload_MongoDB_Driver_IMongoCollectionExtensions_DeleteManyAsync.htm>`__

0 commit comments

Comments
 (0)