mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 04:35:29 +08:00
Update mongodb
This commit is contained in:
parent
d7825aeaf1
commit
d281857b55
@ -0,0 +1,40 @@
|
|||||||
|
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 BulkWriteHandler : IMongoOperationHandler
|
||||||
|
{
|
||||||
|
public HandlerContext context { get; set; }
|
||||||
|
public string operation { get; set; }
|
||||||
|
public int Handle(IMongoCollection<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var documents = ParseJsonArray(json);
|
||||||
|
var bulkOps = new List<WriteModel<BsonDocument>>();
|
||||||
|
|
||||||
|
foreach (var doc in documents)
|
||||||
|
{
|
||||||
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
|
var update = doc["update"].AsBsonDocument;
|
||||||
|
|
||||||
|
var op = new UpdateManyModel<BsonDocument>(filter, update);
|
||||||
|
bulkOps.Add(op);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = collection.BulkWrite(bulkOps);
|
||||||
|
return (int)result.ModifiedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<BsonDocument> ParseJsonArray(string json)
|
||||||
|
{
|
||||||
|
if (json.TrimStart().StartsWith("["))
|
||||||
|
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
||||||
|
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -15,6 +15,7 @@ namespace MongoDb.Ado.data
|
|||||||
{ "insertmany", new InsertManyHandler() },
|
{ "insertmany", new InsertManyHandler() },
|
||||||
{ "update", new UpdateHandler() },
|
{ "update", new UpdateHandler() },
|
||||||
{ "updatemany", new UpdateManyHandler() },
|
{ "updatemany", new UpdateManyHandler() },
|
||||||
|
{ "BulkWrite", new BulkWriteHandler() },
|
||||||
{ "delete", new DeleteHandler() },
|
{ "delete", new DeleteHandler() },
|
||||||
{ "deletemany", new DeleteManyHandler() },
|
{ "deletemany", new DeleteManyHandler() },
|
||||||
{ "find", new NonFindHandler() }
|
{ "find", new NonFindHandler() }
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
using MongoDB.Bson.Serialization;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace MongoDb.Ado.data
|
||||||
|
{
|
||||||
|
public class BulkWriteHandlerAsync : IMongoOperationHandlerAsync
|
||||||
|
{
|
||||||
|
public CancellationToken token { get; set; }
|
||||||
|
public string operation { get; set; }
|
||||||
|
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var documents = ParseJsonArray(json);
|
||||||
|
var bulkOps = new List<WriteModel<BsonDocument>>();
|
||||||
|
|
||||||
|
foreach (var doc in documents)
|
||||||
|
{
|
||||||
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
|
var update = doc["update"].AsBsonDocument;
|
||||||
|
|
||||||
|
var op = new UpdateManyModel<BsonDocument>(filter, update);
|
||||||
|
bulkOps.Add(op);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result =await collection.BulkWriteAsync(bulkOps);
|
||||||
|
return (int)result.ModifiedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<BsonDocument> ParseJsonArray(string json)
|
||||||
|
{
|
||||||
|
if (json.TrimStart().StartsWith("["))
|
||||||
|
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
||||||
|
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -17,6 +17,7 @@ namespace MongoDb.Ado.data
|
|||||||
{ "insertmany", new InsertManyHandlerAsync() },
|
{ "insertmany", new InsertManyHandlerAsync() },
|
||||||
{ "update", new UpdateHandlerAsync() },
|
{ "update", new UpdateHandlerAsync() },
|
||||||
{ "updatemany", new UpdateManyHandlerAsync() },
|
{ "updatemany", new UpdateManyHandlerAsync() },
|
||||||
|
{ "BulkWrite", new BulkWriteHandlerAsync() },
|
||||||
{ "delete", new DeleteHandlerAsync() },
|
{ "delete", new DeleteHandlerAsync() },
|
||||||
{ "deletemany", new DeleteManyHandlerAsync() },
|
{ "deletemany", new DeleteManyHandlerAsync() },
|
||||||
{ "find", new NonFindHandlerAsync() }
|
{ "find", new NonFindHandlerAsync() }
|
||||||
|
@ -62,7 +62,7 @@ namespace MongoDbTest
|
|||||||
}
|
}
|
||||||
).WhereColumns(it =>new { it.Name,it.Age }).ExecuteCommand();
|
).WhereColumns(it =>new { it.Name,it.Age }).ExecuteCommand();
|
||||||
var list5 = db.Queryable<Student>().Where(it => it.SchoolId == "3").ToList();
|
var list5 = db.Queryable<Student>().Where(it => it.SchoolId == "3").ToList();
|
||||||
if(list5.Count!=1|| list5.First().Name!="yy") Cases.ThrowUnitError(); ;
|
if(list5.Count!=1|| list5.First().Name!="yy") Cases.ThrowUnitError();
|
||||||
}
|
}
|
||||||
[SqlSugar.SugarTable("UnitStudentdghhuesd3z1")]
|
[SqlSugar.SugarTable("UnitStudentdghhuesd3z1")]
|
||||||
public class Student : MongoDbBase
|
public class Student : MongoDbBase
|
||||||
|
@ -76,7 +76,7 @@ namespace SqlSugar.MongoDb
|
|||||||
UpdateByObject(groupList, operations, pks);
|
UpdateByObject(groupList, operations, pks);
|
||||||
}
|
}
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append($"updateMany {tableName} [ ");
|
sb.Append($"BulkWrite {tableName} [ ");
|
||||||
sb.Append(string.Join(", ", operations));
|
sb.Append(string.Join(", ", operations));
|
||||||
sb.Append(" ]");
|
sb.Append(" ]");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user