From e60f23d1eff251e517c9fa3302f5884b5ba11bd5 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 2 Jul 2025 10:13:11 +0800 Subject: [PATCH] Update mongodb --- .../Common/MongoDbMethodUtils.cs | 39 ++++++++++++++++++- .../Common/MongoParsedCommand.cs | 15 +++++++ .../MongoDb.Ado.data/MongoDbCommand.cs | 6 +++ .../MongoDbTest/UnitTest/Insert.cs | 10 +++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoParsedCommand.cs diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoDbMethodUtils.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoDbMethodUtils.cs index a9fcf8763..ab45bc450 100644 --- a/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoDbMethodUtils.cs +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoDbMethodUtils.cs @@ -1,4 +1,5 @@ -using System; +using MongoDb.Ado.Data.Common; +using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; @@ -6,7 +7,41 @@ using System.Threading.Tasks; namespace MongoDb.Ado.data { public class MongoDbMethodUtils - { + { + public static MongoParsedCommand ParseMongoCommand(string command) + { + if (string.IsNullOrWhiteSpace(command)) + throw new ArgumentException("command is null or empty"); + + // 去掉前后空格与结尾分号 + command = command.Trim().TrimEnd(';'); + + // 确保以 db. 开头 + if (!command.StartsWith("db.")) + throw new FormatException("Invalid command format. Must start with db."); + + int dotIndex = command.IndexOf('.', 3); // 找 collection 之后的点 + int parenIndex = command.IndexOf('(', dotIndex); + int braceIndex = command.IndexOf('{', parenIndex); + + if (dotIndex == -1 || parenIndex == -1 || braceIndex == -1) + throw new FormatException("Command format not recognized."); + + string collectionName = command.Substring(3, dotIndex - 3).Trim(); + string operation = command.Substring(dotIndex + 1, parenIndex - dotIndex - 1).Trim(); + + string jsonPart = command.Substring(parenIndex + 1).Trim(); + if (jsonPart.EndsWith(")")) + jsonPart = jsonPart.Substring(0, jsonPart.Length - 1); + + return new MongoParsedCommand + { + CollectionName = collectionName, + Operation = operation, + Json = jsonPart + }; + } + public static void ValidateOperation(string operation) { if (ExecuteHandlerFactory.Items.TryGetValue(operation, out var handler)) diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoParsedCommand.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoParsedCommand.cs new file mode 100644 index 000000000..6b8c0aad3 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/Common/MongoParsedCommand.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MongoDb.Ado.Data.Common +{ + public class MongoParsedCommand + { + public string CollectionName { get; set; } + public string Operation { get; set; } + public string Json { get; set; } + } +} + + diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs index c1976ce33..3baa9c048 100644 --- a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs @@ -120,6 +120,12 @@ namespace MongoDb.Ado.data if (string.IsNullOrWhiteSpace(cmd)) throw new InvalidOperationException("CommandText 不能为空。"); + if (cmd.Trim().StartsWith("db.", StringComparison.OrdinalIgnoreCase)) + { + var cmdInfo= MongoDbMethodUtils.ParseMongoCommand(cmd); + return (cmdInfo.Operation,cmdInfo.CollectionName,cmdInfo.Json); + } + var parts = cmd.Trim().Split(new[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) throw new InvalidOperationException("命令格式错误,应为:操作 集合名 JSON过滤"); diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/Insert.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/Insert.cs index 8e7248a5d..530e4bf89 100644 --- a/Src/Asp.NetCore2/MongoDbTest/UnitTest/Insert.cs +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/Insert.cs @@ -35,6 +35,16 @@ namespace MongoDbTest var data = new Student() { Age = 1, Name = "11", SchoolId = "111", CreateDateTime = DateTime.Now }; db.Insertable(data).ExecuteCommandIdentityIntoEntity(); Console.WriteLine(data.Id);//Get _id by ExecuteCommandIdentityIntoEntity + + db.Ado.ExecuteCommand(@"db.UnitStudent1ddsfhssds3z1.insertMany([ + { + Name: ""adfas"", + SchoolId: ""s"", + Age: 11, + CreateDateTime: ISODate(""2025-07-02T10:07:23.799Z"") + }])"); + var list4=db.Queryable().Where(it => it.Name == "adfas").ToList(); + if(list4.Count!=1) Cases.ThrowUnitError(); } [SqlSugar.SugarTable("UnitStudent1ddsfhssds3z1")] public class Student : MongoDbBase