mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-02-27 16:50:33 +08:00
Update Mongodb
This commit is contained in:
@@ -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) };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user