mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 05:13:27 +08:00
Update Mongodb
This commit is contained in:
parent
aca8693c5d
commit
5ec98af257
@ -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<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var doc = BsonDocument.Parse(json);
|
||||||
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
|
var result = collection.DeleteOne(filter);
|
||||||
|
return (int)result.DeletedCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> 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<BsonDocument> ParseJsonArray(string json)
|
||||||
|
{
|
||||||
|
if (json.TrimStart().StartsWith("["))
|
||||||
|
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
||||||
|
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<string, IMongoOperationHandler> Items = new Dictionary<string, IMongoOperationHandler>(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
{ "insert", new InsertHandler() },
|
||||||
|
{ "insertmany", new InsertManyHandler() },
|
||||||
|
{ "update", new UpdateHandler() },
|
||||||
|
{ "updatemany", new UpdateManyHandler() },
|
||||||
|
{ "delete", new DeleteHandler() },
|
||||||
|
{ "deletemany", new DeleteManyHandler() },
|
||||||
|
{ "find", new FindHandler() }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
|
||||||
|
var result = collection.Find(filter).FirstOrDefault();
|
||||||
|
return 0; // 查询不改变数据库
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> collection, string json);
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var doc = BsonDocument.Parse(json);
|
||||||
|
collection.InsertOne(doc);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var documents = ParseJsonArray(json);
|
||||||
|
collection.InsertMany(documents);
|
||||||
|
return documents.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<BsonDocument> ParseJsonArray(string json)
|
||||||
|
{
|
||||||
|
if (json.TrimStart().StartsWith("["))
|
||||||
|
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
||||||
|
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<BsonDocument> 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<BsonDocument> ParseJsonArray(string json)
|
||||||
|
{
|
||||||
|
if (json.TrimStart().StartsWith("["))
|
||||||
|
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
||||||
|
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -58,82 +58,12 @@ namespace MongoDb.Ado.data
|
|||||||
var (operation, collectionName, json) = ParseCommand(_commandText);
|
var (operation, collectionName, json) = ParseCommand(_commandText);
|
||||||
var collection = GetCollection(collectionName);
|
var collection = GetCollection(collectionName);
|
||||||
|
|
||||||
if (operation == "insert")
|
var handlers = ExecuteHandlerFactory.Items;
|
||||||
{
|
|
||||||
// 处理插入操作
|
|
||||||
var doc = BsonDocument.Parse(json);
|
|
||||||
collection.InsertOne(doc);
|
|
||||||
return 1; // 返回插入成功的文档数
|
|
||||||
}
|
|
||||||
if (operation == "insertmany")
|
|
||||||
{
|
|
||||||
// 处理插入多条记录操作
|
|
||||||
var documents = BsonSerializer.Deserialize<List<BsonDocument>>(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); // 单个更新
|
if (!handlers.TryGetValue(operation, out var handler))
|
||||||
return (int)updateResult.ModifiedCount; // 返回修改的文档数
|
throw new NotSupportedException($"不支持的操作类型: {operation}");
|
||||||
}
|
|
||||||
|
|
||||||
if (operation == "updatemany")
|
return handler.Handle(collection, json);
|
||||||
{
|
|
||||||
var totals = 0;
|
|
||||||
// 处理插入多条记录操作
|
|
||||||
var documents = BsonSerializer.Deserialize<List<BsonDocument>>(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<List<BsonDocument>>(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<BsonDocument>.Empty : BsonDocument.Parse(json);
|
|
||||||
var document = collection.Find(filter).FirstOrDefault(); // 查询操作
|
|
||||||
return 0; // 返回 0,表示查询操作已执行,且没有对数据库做更改
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NotSupportedException("不支持此操作类型。");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object ExecuteScalar()
|
public override object ExecuteScalar()
|
||||||
|
@ -97,6 +97,16 @@ namespace MongoDbTest
|
|||||||
connection);
|
connection);
|
||||||
var value = mongoDbCommand.ExecuteNonQuery();
|
var value = mongoDbCommand.ExecuteNonQuery();
|
||||||
connection.Close();
|
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
|
//ExecuteNonQuery deleteMany
|
||||||
{
|
{
|
||||||
@ -108,16 +118,6 @@ namespace MongoDbTest
|
|||||||
var value = mongoDbCommand.ExecuteNonQuery();
|
var value = mongoDbCommand.ExecuteNonQuery();
|
||||||
connection.Close();
|
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()
|
private static void MongoDbConnectionTest()
|
||||||
|
Loading…
Reference in New Issue
Block a user