From 0256e45a5b6bd251a09cef506117241814765e0f Mon Sep 17 00:00:00 2001 From: Normen Scheiber <46715105+nscheibe@users.noreply.github.com> Date: Mon, 21 Nov 2022 17:48:10 +0100 Subject: [PATCH] Added new clean functionality for MongoDB collections. (#66) --- src/MongoDB.Extensions.sln | 5 + .../FindFluentExtensionsTests.cs | 2 +- src/Prime.Extensions.Tests/Helpers/Bar.cs | 22 +- src/Prime.Extensions.Tests/Helpers/Foo.cs | 18 +- .../MongoCollectionExtensionsTests.cs | 57 ++- .../MongoCollectionFindExtensionsTests.cs | 17 +- .../MongoDatabaseExtensionsTests.cs | 133 +++++-- ...ngleQuery_OriginalMongoDbQueryPrinted.snap | 2 +- ...perations_ReturnsAllMongoDBOperations.snap | 364 +++--------------- ...Operations_ReturnsOneMongoDBOperation.snap | 63 +-- .../MongoCollectionExtensions.cs | 54 +++ .../MongoDatabaseExtensions.cs | 50 ++- src/TestProject.props | 4 +- 13 files changed, 340 insertions(+), 451 deletions(-) diff --git a/src/MongoDB.Extensions.sln b/src/MongoDB.Extensions.sln index 168d8e3..bd7699b 100644 --- a/src/MongoDB.Extensions.sln +++ b/src/MongoDB.Extensions.sln @@ -25,6 +25,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration", "Migration\Migr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration.Tests", "Migration.Tests\Migration.Tests.csproj", "{A7D17D8A-99BC-40AC-92B4-996790F7F26E}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".SolutionFiles", ".SolutionFiles", "{A21A1FF3-25C1-4FE1-994E-E16CDF98BB99}" + ProjectSection(SolutionItems) = preProject + TestProject.props = TestProject.props + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs b/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs index 7eac942..426f027 100644 --- a/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs @@ -27,7 +27,7 @@ public void PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted() // Act string mongodbQuery = barCollection - .Find(bar => bar.Name == "Bar1" || bar.Id == "1234") + .Find(bar => bar.Name == "Bar1" || bar.Value == "1234") .Limit(5) .PrintQuery(); diff --git a/src/Prime.Extensions.Tests/Helpers/Bar.cs b/src/Prime.Extensions.Tests/Helpers/Bar.cs index 19cc1a9..8fa0e28 100644 --- a/src/Prime.Extensions.Tests/Helpers/Bar.cs +++ b/src/Prime.Extensions.Tests/Helpers/Bar.cs @@ -1,14 +1,18 @@ -namespace MongoDB.Prime.Extensions.Tests +using System; + +namespace MongoDB.Prime.Extensions.Tests; + +public class Bar { - public class Bar + public Bar(Guid id, string name, string value) { - public Bar(string name) - { - Name = name; - } + Id = id; + Name = name; + Value = value; + } - public string Id { get; set; } + public Guid Id { get; } - public string Name { get; set; } - } + public string Name { get; } + public string Value { get; } } diff --git a/src/Prime.Extensions.Tests/Helpers/Foo.cs b/src/Prime.Extensions.Tests/Helpers/Foo.cs index 66fa339..d50904c 100644 --- a/src/Prime.Extensions.Tests/Helpers/Foo.cs +++ b/src/Prime.Extensions.Tests/Helpers/Foo.cs @@ -1,13 +1,13 @@ -namespace MongoDB.Prime.Extensions.Tests +namespace MongoDB.Prime.Extensions.Tests; + +public class Foo { - public class Foo + public Foo(string id, string name) { - public Foo(string name) - { - Name = name; - } - - public string Id { get; set; } - public string Name { get; set; } + Id = id; + Name = name; } + + public string Id { get; set; } + public string Name { get; set; } } diff --git a/src/Prime.Extensions.Tests/MongoCollectionExtensionsTests.cs b/src/Prime.Extensions.Tests/MongoCollectionExtensionsTests.cs index 0a55c98..45ba576 100644 --- a/src/Prime.Extensions.Tests/MongoCollectionExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/MongoCollectionExtensionsTests.cs @@ -18,6 +18,61 @@ public MongoCollectionExtensionsTests(MongoResource mongoResource) _mongoDatabase = mongoResource.CreateDatabase(); } + [Fact] + public void CleanCollection_CleanAllDocumentsOfACollection_Cleaned() + { + // Arrange + IMongoCollection barCollection = + _mongoDatabase.GetCollection(); + + barCollection.InsertMany(new[] + { + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar4", "Value4"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"), + }); + + Assert.Equal(7, _mongoDatabase.GetCollection().CountDocuments()); + + // Act + barCollection.CleanCollection(); + + // Assert + Assert.Equal(0, _mongoDatabase.GetCollection().CountDocuments()); + } + + [Fact] + public async Task CleanCollectionAsync_CleanAllDocumentsOfACollection_Cleaned() + { + // Arrange + IMongoCollection barCollection = + _mongoDatabase.GetCollection(); + + barCollection.InsertMany(new[] + { + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar4", "Value4"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"), + }); + + Assert.Equal(7, _mongoDatabase.GetCollection().CountDocuments()); + + // Act + await barCollection.CleanCollectionAsync(); + + // Assert + Assert.Equal(0, _mongoDatabase.GetCollection().CountDocuments()); + } + + [Fact(Skip = "not ready yet.")] public async Task Explain_ExplainSingleQuery_SuccessfullyExplained() { @@ -34,7 +89,7 @@ public async Task Explain_ExplainSingleQuery_SuccessfullyExplained() FilterDefinition filter = Builders.Filter.And( - Builders.Filter.Eq(u => u.Id, "1234"), + Builders.Filter.Eq(u => u.Id, Guid.NewGuid()), Builders.Filter.Eq(b => b.Name, "NN")); // Act diff --git a/src/Prime.Extensions.Tests/MongoCollectionFindExtensionsTests.cs b/src/Prime.Extensions.Tests/MongoCollectionFindExtensionsTests.cs index 54783be..5fe14cc 100644 --- a/src/Prime.Extensions.Tests/MongoCollectionFindExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/MongoCollectionFindExtensionsTests.cs @@ -605,22 +605,7 @@ private IEnumerable CreateRandomBars(int count) $"BarValue-{Guid.NewGuid()}")) .ToList(); } - - private class Bar - { - public Bar(Guid id, string name, string value) - { - Id = id; - Name = name; - Value = value; - } - - public Guid Id { get; } - - public string Name { get; } - public string Value { get; } - } - + #endregion } } diff --git a/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs b/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs index ac8de62..ecd9371 100644 --- a/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs @@ -1,7 +1,7 @@ +using System; using System.Collections.Generic; using System.Linq; -using System.Text.Json; -using System.Text.Json.Nodes; +using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; using Snapshooter.Xunit; @@ -163,20 +163,13 @@ public void GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOper // Assert Snapshot.Match(results.Single(), matchOptions => matchOptions - .IgnoreField("**.ns") - .IgnoreField("**.$db") - .IgnoreField("**.flowControl") - .IgnoreField("**.millis") - .IgnoreField("**.ts") - .IgnoreField("**.base64") - .IgnoreField("**.client") - .IgnoreField("**.locks") - .IgnoreField("**.ReplicationStateTransition") - .IgnoreField("**.FeatureCompatibilityVersion") - .IgnoreField("**.queryHash") - .IgnoreField("**.planCacheKey") - .IgnoreField("**.queryExecutionEngine") - .IgnoreField("**.readConcern") + .IncludeField("**.command") + .IncludeField("**.keysExamined") + .IncludeField("**.docsExamined") + .IncludeField("**.planSummary") + .IncludeField("**.execStats") + .ExcludeField("**.$db") + .ExcludeField("**.lsid") ); } @@ -188,8 +181,9 @@ public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOper _mongoDatabase.CreateCollection("Bar"); _mongoDatabase.CreateCollection("Foo"); - _mongoDatabase.GetCollection().InsertOne(new Bar("bar1")); - _mongoDatabase.GetCollection().InsertOne(new Foo("foo1")); + _mongoDatabase.GetCollection() + .InsertOne(new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1")); + _mongoDatabase.GetCollection().InsertOne(new Foo("Foo1", "Value1")); _mongoDatabase.GetCollection().Find(foo => foo.Name == "foo1").ToList(); // Act @@ -199,23 +193,98 @@ public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOper // Assert Snapshot.Match(results.ToJsonArray(), matchOptions => matchOptions - .IgnoreField("**.ns") - .IgnoreField("**.$db") - .IgnoreField("**.flowControl") - .IgnoreField("**.millis") - .IgnoreField("**.ts") - .IgnoreField("**.base64") - .IgnoreField("**.client") - .IgnoreField("**.locks") - .IgnoreField("**.ReplicationStateTransition") - .IgnoreField("**.FeatureCompatibilityVersion") - .IgnoreField("**.queryHash") - .IgnoreField("**.planCacheKey") - .IgnoreField("**.queryExecutionEngine") - .IgnoreField("**.readConcern") + .IncludeField("**.command") + .IncludeField("**.keysExamined") + .IncludeField("**.docsExamined") + .IncludeField("**.planSummary") + .IncludeField("**.execStats") + .ExcludeField("**.$db") + .ExcludeField("**.lsid") ); } #endregion + + #region CleanAllCollections Tests + + [Fact] + public async Task CleanAllCollections_CleanAllDocumentsOfAllCollections_AllCollectionsEmpty() + { + // Arrange + IMongoCollection barCollection = _mongoDatabase.GetCollection(); + IMongoCollection fooCollection = _mongoDatabase.GetCollection(); + + var arrangedBars = new List { + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar3", "Value4"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"), + }; + + var arrangedFoo = new List { + new Foo("Foo1", "Value1"), + new Foo("Foo2", "Value2"), + new Foo("Foo3", "Value3"), + new Foo("Foo4", "Value4"), + new Foo("Foo5", "Value5") + }; + + await barCollection.InsertManyAsync(arrangedBars); + await fooCollection.InsertManyAsync(arrangedFoo); + + Assert.Equal(7, barCollection.CountDocuments()); + Assert.Equal(5, fooCollection.CountDocuments()); + + // Act + _mongoDatabase.CleanAllCollections(); + + // Assert + Assert.Equal(0, barCollection.CountDocuments()); + Assert.Equal(0, fooCollection.CountDocuments()); + } + + [Fact] + public async Task CleanAllCollectionsAsync_CleanAllDocumentsOfAllCollections_AllCollectionsEmpty() + { + // Arrange + IMongoCollection barCollection = _mongoDatabase.GetCollection(); + IMongoCollection fooCollection = _mongoDatabase.GetCollection(); + + var arrangedBars = new List { + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar3", "Value4"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"), + }; + + var arrangedFoo = new List { + new Foo("Foo1", "Value1"), + new Foo("Foo2", "Value2"), + new Foo("Foo3", "Value3"), + new Foo("Foo4", "Value4"), + new Foo("Foo5", "Value5") + }; + + await barCollection.InsertManyAsync(arrangedBars); + await fooCollection.InsertManyAsync(arrangedFoo); + + Assert.Equal(7, barCollection.CountDocuments()); + Assert.Equal(5, fooCollection.CountDocuments()); + + // Act + await _mongoDatabase.CleanAllCollectionsAsync(); + + // Assert + Assert.Equal(0, barCollection.CountDocuments()); + Assert.Equal(0, fooCollection.CountDocuments()); + } + + #endregion CleanAllCollections Tests } } diff --git a/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted.snap b/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted.snap index 5290c28..16ec922 100644 --- a/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted.snap +++ b/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted.snap @@ -1 +1 @@ -find({ "$or" : [{ "Name" : "Bar1" }, { "_id" : "1234" }] }).limit(5) +find({ "$or" : [{ "Name" : "Bar1" }, { "Value" : "1234" }] }).limit(5) diff --git a/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOperations.snap b/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOperations.snap index b9a8b5d..4c7befb 100644 --- a/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOperations.snap +++ b/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOperations.snap @@ -1,329 +1,57 @@ -[{ - "op": "command", - "ns": "db_98188f7406cc40a888db9ab8056eb396.Bar", - "command": { - "create": "Bar", - "$db": "db_98188f7406cc40a888db9ab8056eb396", - "lsid": { - "id": { - "$binary": { - "base64": "w6P\u002BgezU1ECJmuzdA0rhPg==", - "subType": "03" - } - } +[ + { + "command": { + "create": "Bar" } }, - "numYield": 0, - "locks": { - "ParallelBatchWriterMode": { - "acquireCount": { - "r": 2 - } - }, - "ReplicationStateTransition": { - "acquireCount": { - "w": 2 - } - }, - "Global": { - "acquireCount": { - "r": 1, - "w": 1 - } - }, - "Database": { - "acquireCount": { - "r": 1, - "w": 1 - } - }, - "Collection": { - "acquireCount": { - "w": 1 - } - }, - "Mutex": { - "acquireCount": { - "r": 2 - } + { + "command": { + "create": "Foo" } }, - "flowControl": { - "acquireCount": 1, - "timeAcquiringMicros": 5 - }, - "responseLength": 38, - "protocol": "op_msg", - "millis": 18, - "ts": { - "$date": "2022-10-24T12:34:51.893Z" - }, - "client": "172.17.0.1", - "allUsers": [], - "user": "" -},{ - "op": "command", - "ns": "db_98188f7406cc40a888db9ab8056eb396.Foo", - "command": { - "create": "Foo", - "$db": "db_98188f7406cc40a888db9ab8056eb396", - "lsid": { - "id": { - "$binary": { - "base64": "w6P\u002BgezU1ECJmuzdA0rhPg==", - "subType": "03" - } - } + { + "command": { + "insert": "Bar", + "ordered": true } }, - "numYield": 0, - "locks": { - "ParallelBatchWriterMode": { - "acquireCount": { - "r": 2 - } - }, - "ReplicationStateTransition": { - "acquireCount": { - "w": 2 - } - }, - "Global": { - "acquireCount": { - "r": 1, - "w": 1 - } - }, - "Database": { - "acquireCount": { - "r": 1, - "w": 1 - } - }, - "Collection": { - "acquireCount": { - "w": 1 - } - }, - "Mutex": { - "acquireCount": { - "r": 2 - } - } - }, - "flowControl": { - "acquireCount": 1, - "timeAcquiringMicros": 4 - }, - "responseLength": 38, - "protocol": "op_msg", - "millis": 16, - "ts": { - "$date": "2022-10-24T12:34:51.921Z" - }, - "client": "172.17.0.1", - "allUsers": [], - "user": "" -},{ - "op": "insert", - "ns": "db_98188f7406cc40a888db9ab8056eb396.Bar", - "command": { - "insert": "Bar", - "ordered": true, - "$db": "db_98188f7406cc40a888db9ab8056eb396", - "lsid": { - "id": { - "$binary": { - "base64": "w6P\u002BgezU1ECJmuzdA0rhPg==", - "subType": "03" - } - } + { + "command": { + "insert": "Foo", + "ordered": true } }, - "ninserted": 1, - "keysInserted": 1, - "numYield": 0, - "locks": { - "ParallelBatchWriterMode": { - "acquireCount": { - "r": 1 - } - }, - "ReplicationStateTransition": { - "acquireCount": { - "w": 1 + { + "command": { + "find": "Foo", + "filter": { + "Name": "foo1" + }, + "readConcern": { + "level": "majority" } }, - "Global": { - "acquireCount": { - "w": 1 - } - }, - "Database": { - "acquireCount": { - "w": 1 - } - }, - "Collection": { - "acquireCount": { - "w": 1 - } - }, - "Mutex": { - "acquireCount": { - "r": 1 - } - } - }, - "flowControl": { - "acquireCount": 1, - "timeAcquiringMicros": 5 - }, - "responseLength": 45, - "protocol": "op_msg", - "millis": 0, - "ts": { - "$date": "2022-10-24T12:34:52.026Z" - }, - "client": "172.17.0.1", - "allUsers": [], - "user": "" -},{ - "op": "insert", - "ns": "db_98188f7406cc40a888db9ab8056eb396.Foo", - "command": { - "insert": "Foo", - "ordered": true, - "$db": "db_98188f7406cc40a888db9ab8056eb396", - "lsid": { - "id": { - "$binary": { - "base64": "w6P\u002BgezU1ECJmuzdA0rhPg==", - "subType": "03" - } - } - } - }, - "ninserted": 1, - "keysInserted": 1, - "numYield": 0, - "locks": { - "ParallelBatchWriterMode": { - "acquireCount": { - "r": 1 - } - }, - "ReplicationStateTransition": { - "acquireCount": { - "w": 1 - } - }, - "Global": { - "acquireCount": { - "w": 1 - } - }, - "Database": { - "acquireCount": { - "w": 1 - } - }, - "Collection": { - "acquireCount": { - "w": 1 - } - }, - "Mutex": { - "acquireCount": { - "r": 1 - } - } - }, - "flowControl": { - "acquireCount": 1, - "timeAcquiringMicros": 4 - }, - "responseLength": 45, - "protocol": "op_msg", - "millis": 0, - "ts": { - "$date": "2022-10-24T12:34:52.041Z" - }, - "client": "172.17.0.1", - "allUsers": [], - "user": "" -},{ - "op": "query", - "ns": "db_98188f7406cc40a888db9ab8056eb396.Foo", - "command": { - "find": "Foo", - "filter": { - "Name": "foo1" - }, - "readConcern": { - "level": "majority" - }, - "$db": "db_98188f7406cc40a888db9ab8056eb396", - "lsid": { - "id": { - "$binary": { - "base64": "w6P\u002BgezU1ECJmuzdA0rhPg==", - "subType": "03" + "keysExamined": 0, + "docsExamined": 1, + "planSummary": "COLLSCAN", + "execStats": { + "stage": "COLLSCAN", + "filter": { + "Name": { + "$eq": "foo1" } - } - } - }, - "keysExamined": 0, - "docsExamined": 1, - "cursorExhausted": true, - "numYield": 0, - "nreturned": 1, - "queryHash": "EBFEE4C5", - "planCacheKey": "CEC3A330", - "locks": { - "Global": { - "acquireCount": { - "r": 1 - } - }, - "Mutex": { - "acquireCount": { - "r": 1 - } - } - }, - "flowControl": {}, - "readConcern": { - "level": "majority", - "provenance": "clientSupplied" - }, - "responseLength": 156, - "protocol": "op_msg", - "millis": 0, - "planSummary": "COLLSCAN", - "execStats": { - "stage": "COLLSCAN", - "filter": { - "Name": { - "$eq": "foo1" - } - }, - "nReturned": 1, - "executionTimeMillisEstimate": 0, - "works": 3, - "advanced": 1, - "needTime": 1, - "needYield": 0, - "saveState": 0, - "restoreState": 0, - "isEOF": 1, - "direction": "forward", - "docsExamined": 1 - }, - "ts": { - "$date": "2022-10-24T12:34:52.088Z" - }, - "client": "172.17.0.1", - "allUsers": [], - "user": "" -}] + }, + "nReturned": 0, + "executionTimeMillisEstimate": 0, + "works": 3, + "advanced": 0, + "needTime": 2, + "needYield": 0, + "saveState": 0, + "restoreState": 0, + "isEOF": 1, + "direction": "forward", + "docsExamined": 1 + } + } +] diff --git a/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOperation.snap b/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOperation.snap index cba0c00..eb5ac7f 100644 --- a/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOperation.snap +++ b/src/Prime.Extensions.Tests/__snapshots__/MongoDatabaseExtensionsTests.GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOperation.snap @@ -1,64 +1,5 @@ { - "op": "command", - "ns": "db_2073f5d0f83849bb991287c1b488d141.Bar", "command": { - "create": "Bar", - "$db": "db_2073f5d0f83849bb991287c1b488d141", - "lsid": { - "id": { - "$binary": { - "base64": "LS6ugmsi2EiKMKvM62WMng==", - "subType": "03" - } - } - } - }, - "numYield": 0, - "locks": { - "ParallelBatchWriterMode": { - "acquireCount": { - "r": 2 - } - }, - "ReplicationStateTransition": { - "acquireCount": { - "w": 2 - } - }, - "Global": { - "acquireCount": { - "r": 1, - "w": 1 - } - }, - "Database": { - "acquireCount": { - "r": 1, - "w": 1 - } - }, - "Collection": { - "acquireCount": { - "w": 1 - } - }, - "Mutex": { - "acquireCount": { - "r": 2 - } - } - }, - "flowControl": { - "acquireCount": 1, - "timeAcquiringMicros": 3 - }, - "responseLength": 38, - "protocol": "op_msg", - "millis": 17, - "ts": { - "$date": "2022-10-24T12:44:09.669Z" - }, - "client": "172.17.0.1", - "allUsers": [], - "user": "" + "create": "Bar" + } } diff --git a/src/Prime.Extensions/MongoCollectionExtensions.cs b/src/Prime.Extensions/MongoCollectionExtensions.cs index a89f3cf..12c2917 100644 --- a/src/Prime.Extensions/MongoCollectionExtensions.cs +++ b/src/Prime.Extensions/MongoCollectionExtensions.cs @@ -8,6 +8,60 @@ namespace MongoDB.Prime.Extensions { public static class MongoCollectionExtensions { + /// + /// Deletes all entries within a collection. + /// The collection is not dropped and all indexes stay. + /// + /// The type of the document. + /// The collection to clean. + public static void CleanCollection( + this IMongoCollection collection) + { + collection.DeleteMany( + FilterDefinition.Empty); + } + + /// + /// Deletes all entries within a collection. + /// The collection is not dropped and all indexes stay. + /// + /// The type of the document. + /// The collection to clean. + public static async Task CleanCollectionAsync( + this IMongoCollection collection, + CancellationToken cancellationToken = default) + { + await collection.DeleteManyAsync( + FilterDefinition.Empty, + cancellationToken: cancellationToken); + } + + /// + /// Counts all the documents of a collection + /// + /// The document count. + public static long CountDocuments( + this IMongoCollection collection, + CountOptions? options = null, + CancellationToken cancellationToken = default) + { + return collection.CountDocuments( + FilterDefinition.Empty, options, cancellationToken); + } + + /// + /// Counts all the documents of a collection + /// + /// The document count. + public static Task CountDocumentsAsync( + this IMongoCollection collection, + CountOptions? options = null, + CancellationToken cancellationToken = default) + { + return collection.CountDocumentsAsync( + FilterDefinition.Empty, options, cancellationToken); + } + public static Task InsertOneAsync( this IMongoCollection collection, TDocument document, diff --git a/src/Prime.Extensions/MongoDatabaseExtensions.cs b/src/Prime.Extensions/MongoDatabaseExtensions.cs index 17012f1..2c1445f 100644 --- a/src/Prime.Extensions/MongoDatabaseExtensions.cs +++ b/src/Prime.Extensions/MongoDatabaseExtensions.cs @@ -1,7 +1,9 @@ -using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Driver; @@ -68,6 +70,14 @@ public static IEnumerable GetProfiledOperations( return normalizedJson; } + /// + /// Gets the collection by Type name. The name of the collection + /// will be the typeof(TDocument).Name. + /// + /// The type of document. + /// The mongo database. + /// The mongo collection settings. + /// The mongo collection with the name of the document type. public static IMongoCollection GetCollection( this IMongoDatabase mongoDatabase, MongoCollectionSettings? settings = null) @@ -75,5 +85,43 @@ public static IMongoCollection GetCollection( return mongoDatabase .GetCollection(typeof(TDocument).Name, settings); } + + /// + /// Deletes all entries of every collection of the mongo database. + /// The collections will NOT be dropped and the indexes stay unmodified. + /// + /// The database to clean the collections. + public static void CleanAllCollections( + this IMongoDatabase mongoDatabase) + { + foreach (var name in mongoDatabase.ListCollectionNames().ToList()) + { + IMongoCollection collection = + mongoDatabase.GetCollection(name); + + collection.CleanCollection(); + } + } + + /// + /// Deletes all entries of every collection of the mongo database. + /// The collections will NOT be dropped and the indexes stay unmodified. + /// + /// The database to clean the collections. + public static async Task CleanAllCollectionsAsync( + this IMongoDatabase mongoDatabase, + CancellationToken cancellationToken = default) + { + IAsyncCursor cursor = await mongoDatabase + .ListCollectionNamesAsync(cancellationToken: cancellationToken); + + foreach (var name in await cursor.ToListAsync(cancellationToken)) + { + IMongoCollection collection = + mongoDatabase.GetCollection(name); + + await collection.CleanCollectionAsync(cancellationToken); + } + } } } diff --git a/src/TestProject.props b/src/TestProject.props index 257f5d3..6d5a5f8 100644 --- a/src/TestProject.props +++ b/src/TestProject.props @@ -9,10 +9,10 @@ - + - + all