From 5ec98af257771d640bfab340b41bebc83ef6ca41 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 2 May 2025 07:54:16 +0800 Subject: [PATCH] Update Mongodb --- .../ExecuteNonQueryItems/DeleteHandler.cs | 19 +++++ .../ExecuteNonQueryItems/DeleteManyHandler.cs | 32 ++++++++ .../ExecuteHandlerFactory.cs | 21 +++++ .../ExecuteNonQueryItems/FindHandler.cs | 18 +++++ .../IMongoOperationHandler.cs | 13 ++++ .../ExecuteNonQueryItems/InsertHandler.cs | 18 +++++ .../ExecuteNonQueryItems/InsertManyHandler.cs | 26 +++++++ .../ExecuteNonQueryItems/UpdateHandler.cs | 20 +++++ .../ExecuteNonQueryItems/UpdateManyHandler.cs | 34 ++++++++ .../MongoDb.Ado.data/MongoDbCommand.cs | 78 +------------------ Src/Asp.NetCore2/MongoDbTest/AdoTest.cs | 20 ++--- 11 files changed, 215 insertions(+), 84 deletions(-) create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteManyHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/ExecuteHandlerFactory.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/FindHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/IMongoOperationHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertManyHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateHandler.cs create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateManyHandler.cs diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteHandler.cs new file mode 100644 index 000000000..25bc2c1cd --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteHandler.cs @@ -0,0 +1,19 @@ +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class DeleteHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var doc = BsonDocument.Parse(json); + var filter = doc["filter"].AsBsonDocument; + var result = collection.DeleteOne(filter); + return (int)result.DeletedCount; + } + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteManyHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteManyHandler.cs new file mode 100644 index 000000000..1e5c1a88b --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/DeleteManyHandler.cs @@ -0,0 +1,32 @@ +using MongoDB.Bson.Serialization; +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class DeleteManyHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var documents = ParseJsonArray(json); + int total = 0; + foreach (var doc in documents) + { + var filter = doc["filter"].AsBsonDocument; + var result = collection.DeleteMany(filter); + total += (int)result.DeletedCount; + } + return total; + } + + private List ParseJsonArray(string json) + { + if (json.TrimStart().StartsWith("[")) + return BsonSerializer.Deserialize>(json); + return new List { BsonDocument.Parse(json) }; + } + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/ExecuteHandlerFactory.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/ExecuteHandlerFactory.cs new file mode 100644 index 000000000..3c3289181 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/ExecuteHandlerFactory.cs @@ -0,0 +1,21 @@ +using MongoDb.Ado.data; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class ExecuteHandlerFactory + { + public readonly static Dictionary Items = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "insert", new InsertHandler() }, + { "insertmany", new InsertManyHandler() }, + { "update", new UpdateHandler() }, + { "updatemany", new UpdateManyHandler() }, + { "delete", new DeleteHandler() }, + { "deletemany", new DeleteManyHandler() }, + { "find", new FindHandler() } + }; + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/FindHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/FindHandler.cs new file mode 100644 index 000000000..f555b1b4a --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/FindHandler.cs @@ -0,0 +1,18 @@ +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class FindHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition.Empty : BsonDocument.Parse(json); + var result = collection.Find(filter).FirstOrDefault(); + return 0; // 查询不改变数据库 + } + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/IMongoOperationHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/IMongoOperationHandler.cs new file mode 100644 index 000000000..b56aadb63 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/IMongoOperationHandler.cs @@ -0,0 +1,13 @@ +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public interface IMongoOperationHandler + { + int Handle(IMongoCollection collection, string json); + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertHandler.cs new file mode 100644 index 000000000..c2695313d --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertHandler.cs @@ -0,0 +1,18 @@ +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class InsertHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var doc = BsonDocument.Parse(json); + collection.InsertOne(doc); + return 1; + } + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertManyHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertManyHandler.cs new file mode 100644 index 000000000..60c974cfe --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertManyHandler.cs @@ -0,0 +1,26 @@ +using MongoDB.Bson.Serialization; +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class InsertManyHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var documents = ParseJsonArray(json); + collection.InsertMany(documents); + return documents.Count; + } + + private List ParseJsonArray(string json) + { + if (json.TrimStart().StartsWith("[")) + return BsonSerializer.Deserialize>(json); + return new List { BsonDocument.Parse(json) }; + } + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateHandler.cs new file mode 100644 index 000000000..2091fe01a --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateHandler.cs @@ -0,0 +1,20 @@ +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class UpdateHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var doc = BsonDocument.Parse(json); + var filter = doc["filter"].AsBsonDocument; + var update = doc["update"].AsBsonDocument; + var result = collection.UpdateOne(filter, update); + return (int)result.ModifiedCount; + } + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateManyHandler.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateManyHandler.cs new file mode 100644 index 000000000..fe2a25277 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/UpdateManyHandler.cs @@ -0,0 +1,34 @@ +using MongoDB.Bson.Serialization; +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.data +{ + public class UpdateManyHandler : IMongoOperationHandler + { + public int Handle(IMongoCollection collection, string json) + { + var documents = ParseJsonArray(json); + int total = 0; + foreach (var doc in documents) + { + var filter = doc["filter"].AsBsonDocument; + var update = doc["update"].AsBsonDocument; + var result = collection.UpdateMany(filter, update); + total += (int)result.ModifiedCount; + } + return total; + } + + private List ParseJsonArray(string json) + { + if (json.TrimStart().StartsWith("[")) + return BsonSerializer.Deserialize>(json); + return new List { BsonDocument.Parse(json) }; + } + } + +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs index abc9ed4f3..2007e40c8 100644 --- a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs @@ -58,82 +58,12 @@ namespace MongoDb.Ado.data var (operation, collectionName, json) = ParseCommand(_commandText); var collection = GetCollection(collectionName); - if (operation == "insert") - { - // 处理插入操作 - var doc = BsonDocument.Parse(json); - collection.InsertOne(doc); - return 1; // 返回插入成功的文档数 - } - if (operation == "insertmany") - { - // 处理插入多条记录操作 - var documents = BsonSerializer.Deserialize>(json); // 假设 json 是包含多个文档的数组 - collection.InsertMany(documents); - return documents.Count; // 返回插入成功的文档数 - } - if (operation == "update") - { - // 处理更新操作 - var updateCommand = BsonDocument.Parse(json); - var filter = updateCommand["filter"].AsBsonDocument; - var update = updateCommand["update"].AsBsonDocument; - var options = updateCommand.Contains("options") ? updateCommand["options"].AsBsonDocument : null; + var handlers = ExecuteHandlerFactory.Items; - var updateResult = collection.UpdateOne(filter, update); // 单个更新 - return (int)updateResult.ModifiedCount; // 返回修改的文档数 - } + if (!handlers.TryGetValue(operation, out var handler)) + throw new NotSupportedException($"不支持的操作类型: {operation}"); - if (operation == "updatemany") - { - var totals = 0; - // 处理插入多条记录操作 - var documents = BsonSerializer.Deserialize>(json); // 假设 json 是包含多个文档的数组 - foreach (var updateCommand in documents) - { - var filter = updateCommand["filter"].AsBsonDocument; - var update = updateCommand["update"].AsBsonDocument; - var options = updateCommand.Contains("options") ? updateCommand["options"].AsBsonDocument : null; - - var updateResult = collection.UpdateMany(filter, update); // 单个更新 - totals+=(int)updateResult.ModifiedCount; // 返回修改的文档数 - } - return documents.Count; // 返回插入成功的文档数 - } - - if (operation == "delete") - { - // 处理删除单个文档操作 - var deleteCommand = BsonDocument.Parse(json); - var filter = deleteCommand["filter"].AsBsonDocument; - - var deleteResult = collection.DeleteOne(filter); // 单个删除 - return (int)deleteResult.DeletedCount; // 返回删除的文档数 - } - - if (operation == "deletemany") - { - var totals = 0; - // 处理插入多条记录操作 - var documents = BsonSerializer.Deserialize>(json); // 假设 json - foreach (var updateCommand in documents) - { - var filter = updateCommand["filter"].AsBsonDocument; - var updateResult = collection.DeleteMany(filter); // 单个更新 - totals += (int)updateResult.DeletedCount; // 返回修改的文档数 - } - return documents.Count; // 返回插入成功的文档数 - } - - if (operation == "find") - { - // 处理查询操作,查询并返回 0 - var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition.Empty : BsonDocument.Parse(json); - var document = collection.Find(filter).FirstOrDefault(); // 查询操作 - return 0; // 返回 0,表示查询操作已执行,且没有对数据库做更改 - } - - throw new NotSupportedException("不支持此操作类型。"); + return handler.Handle(collection, json); } public override object ExecuteScalar() diff --git a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs index c05b11b42..646755903 100644 --- a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs +++ b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs @@ -97,6 +97,16 @@ namespace MongoDbTest connection); var value = mongoDbCommand.ExecuteNonQuery(); connection.Close(); + } + //ExecuteNonQuery delete + { + var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString); + connection.Open(); + MongoDbCommand mongoDbCommand = new MongoDbCommand( + "delete b {\"filter\":{ name: \"John\" }}", + connection); + var value = mongoDbCommand.ExecuteNonQuery(); + connection.Close(); } //ExecuteNonQuery deleteMany { @@ -108,16 +118,6 @@ namespace MongoDbTest var value = mongoDbCommand.ExecuteNonQuery(); connection.Close(); } - //ExecuteNonQuery delete - { - var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString); - connection.Open(); - MongoDbCommand mongoDbCommand = new MongoDbCommand( - "delete b {\"filter\":{ name: \"John\" }}", - connection); - var value = mongoDbCommand.ExecuteNonQuery(); - connection.Close(); - } } private static void MongoDbConnectionTest()