diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs index 282924dbb..fa5f7945c 100644 --- a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs @@ -4,6 +4,8 @@ using System.Data; using MongoDB.Bson; using MongoDB.Driver; using System.Linq; +using MongoDB.Bson.Serialization; +using System.Collections.Generic; namespace MongoDb.Ado.data { @@ -58,12 +60,71 @@ namespace MongoDb.Ado.data if (operation == "insert") { + // 处理插入操作 var doc = BsonDocument.Parse(json); collection.InsertOne(doc); - return 1; + 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 updateResult = collection.UpdateOne(filter, update); // 单个更新 + return (int)updateResult.ModifiedCount; // 返回修改的文档数 } - throw new NotSupportedException("只支持 insert 操作。"); + if (operation == "updatemany") + { + // 处理更新多个文档操作 + 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 updateResult = collection.UpdateMany(filter, update); // 多个更新 + return (int)updateResult.ModifiedCount; // 返回修改的文档数 + } + + 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 deleteCommand = BsonDocument.Parse(json); + var filter = deleteCommand["filter"].AsBsonDocument; + + var deleteResult = collection.DeleteMany(filter); // 多个删除 + return (int)deleteResult.DeletedCount; // 返回删除的文档数 + } + + 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("不支持此操作类型。"); } public override object ExecuteScalar() diff --git a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs index 3af14207f..9723fa137 100644 --- a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs +++ b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs @@ -44,6 +44,14 @@ namespace MongoDbTest var value=mongoDbCommand.ExecuteScalar(); connection.Close(); } + //ExecuteNonQuery + { + var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString); + connection.Open(); + MongoDbCommand mongoDbCommand = new MongoDbCommand(" insertMany b [{ name: \"John\", age: 31 }, { name: \"Alice\", age: 25 }, { name: \"Bob\", age: 30 } ] ", connection); + var value = mongoDbCommand.ExecuteNonQuery(); + connection.Close(); + } } private static void MongoDbConnectionTest()