Update mongodb

This commit is contained in:
sunkaixuan
2025-07-11 17:20:42 +08:00
parent 9883aeca4e
commit ce87e40d41
18 changed files with 154 additions and 29 deletions

View File

@@ -25,8 +25,16 @@ namespace MongoDb.Ado.data
bulkOps.Add(op); bulkOps.Add(op);
} }
if (bulkOps.Count == 0) return 0; if (bulkOps.Count == 0) return 0;
var result = collection.BulkWrite(bulkOps); if (context.IsAnyServerSession)
return (int)result.ModifiedCount; {
var result = collection.BulkWrite(context.ServerSession,bulkOps);
return (int)result.ModifiedCount;
}
else
{
var result = collection.BulkWrite(bulkOps);
return (int)result.ModifiedCount;
}
} }
private List<BsonDocument> ParseJsonArray(string json) private List<BsonDocument> ParseJsonArray(string json)

View File

@@ -15,8 +15,16 @@ namespace MongoDb.Ado.data
{ {
var doc = BsonDocument.Parse(json); var doc = BsonDocument.Parse(json);
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var result = collection.DeleteOne(filter); if (context.IsAnyServerSession)
return (int)result.DeletedCount; {
var result = collection.DeleteOne(context.ServerSession,filter);
return (int)result.DeletedCount;
}
else
{
var result = collection.DeleteOne(filter);
return (int)result.DeletedCount;
}
} }
} }
} }

View File

@@ -18,8 +18,16 @@ namespace MongoDb.Ado.data
foreach (var doc in documents) foreach (var doc in documents)
{ {
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var result = collection.DeleteMany(filter); if (context.IsAnyServerSession)
total += (int)result.DeletedCount; {
var result = collection.DeleteMany(context.ServerSession,filter);
total += (int)result.DeletedCount;
}
else
{
var result = collection.DeleteMany(filter);
total += (int)result.DeletedCount;
}
} }
return total; return total;
} }

View File

@@ -13,7 +13,14 @@ namespace MongoDb.Ado.data
public int Handle(IMongoCollection<BsonDocument> collection, string json) public int Handle(IMongoCollection<BsonDocument> collection, string json)
{ {
var doc = BsonDocument.Parse(json); var doc = BsonDocument.Parse(json);
collection.InsertOne(doc); if (context.IsAnyServerSession)
{
collection.InsertOne(context.ServerSession,doc);
}
else
{
collection.InsertOne(doc);
}
var objectId = doc["_id"].AsObjectId.ToString(); var objectId = doc["_id"].AsObjectId.ToString();
context.ids = new string[] { objectId }; context.ids = new string[] { objectId };
return 1; return 1;

View File

@@ -15,7 +15,14 @@ namespace MongoDb.Ado.data
public int Handle(IMongoCollection<BsonDocument> collection, string json) public int Handle(IMongoCollection<BsonDocument> collection, string json)
{ {
var documents = ParseJsonArray(json); var documents = ParseJsonArray(json);
collection.InsertMany(documents); if (context.IsAnyServerSession)
{
collection.InsertMany(context.ServerSession,documents);
}
else
{
collection.InsertMany(documents);
}
var objectIds = documents.Select(it=>it["_id"].AsObjectId.ToString()).ToArray(); var objectIds = documents.Select(it=>it["_id"].AsObjectId.ToString()).ToArray();
context.ids = objectIds; context.ids = objectIds;
return documents.Count; return documents.Count;

View File

@@ -15,8 +15,17 @@ namespace MongoDb.Ado.data
var doc = BsonDocument.Parse(json); var doc = BsonDocument.Parse(json);
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var update = doc["update"].AsBsonDocument; var update = doc["update"].AsBsonDocument;
var result = collection.UpdateOne(filter, update); if (context.IsAnyServerSession)
return (int)result.ModifiedCount; {
var result = collection.UpdateOne(context.ServerSession,filter, update);
return (int)result.ModifiedCount;
}
else
{
var result = collection.UpdateOne(filter, update);
return (int)result.ModifiedCount;
}
} }
} }
} }

View File

@@ -19,8 +19,16 @@ namespace MongoDb.Ado.data
{ {
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var update = doc["update"].AsBsonDocument; var update = doc["update"].AsBsonDocument;
var result = collection.UpdateMany(filter, update); if (context.IsAnyServerSession)
total += (int)result.ModifiedCount; {
var result = collection.UpdateMany(context.ServerSession,filter, update);
total += (int)result.ModifiedCount;
}
else
{
var result = collection.UpdateMany(filter, update);
total += (int)result.ModifiedCount;
}
} }
return total; return total;
} }

View File

@@ -28,8 +28,16 @@ namespace MongoDb.Ado.data
bulkOps.Add(op); bulkOps.Add(op);
} }
if (bulkOps.Count == 0) return 0; if (bulkOps.Count == 0) return 0;
var result =await collection.BulkWriteAsync(bulkOps); if (context.IsAnyServerSession)
return (int)result.ModifiedCount; {
var result = await collection.BulkWriteAsync(context.ServerSession,bulkOps);
return (int)result.ModifiedCount;
}
else
{
var result = await collection.BulkWriteAsync(bulkOps);
return (int)result.ModifiedCount;
}
} }
private List<BsonDocument> ParseJsonArray(string json) private List<BsonDocument> ParseJsonArray(string json)

View File

@@ -17,8 +17,16 @@ namespace MongoDb.Ado.data
{ {
var doc = BsonDocument.Parse(json); var doc = BsonDocument.Parse(json);
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var result =await collection.DeleteOneAsync(filter,token); if (context.IsAnyServerSession)
return (int)result.DeletedCount; {
var result = await collection.DeleteOneAsync(context.ServerSession,filter,null,token);
return (int)result.DeletedCount;
}
else
{
var result = await collection.DeleteOneAsync(filter, token);
return (int)result.DeletedCount;
}
} }
} }
} }

View File

@@ -21,8 +21,16 @@ namespace MongoDb.Ado.data
foreach (var doc in documents) foreach (var doc in documents)
{ {
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var result =await collection.DeleteManyAsync(filter,token); if (context.IsAnyServerSession)
total += (int)result.DeletedCount; {
var result = await collection.DeleteManyAsync(context.ServerSession,filter,null, token);
total += (int)result.DeletedCount;
}
else
{
var result = await collection.DeleteManyAsync(filter, token);
total += (int)result.DeletedCount;
}
} }
return total; return total;
} }

View File

@@ -16,7 +16,14 @@ namespace MongoDb.Ado.data
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{ {
var doc = BsonDocument.Parse(json); var doc = BsonDocument.Parse(json);
await collection.InsertOneAsync(doc,null,token); if (context.IsAnyServerSession)
{
await collection.InsertOneAsync(context.ServerSession,doc, null, token);
}
else
{
await collection.InsertOneAsync(doc, null, token);
}
var objectId = doc["_id"].AsObjectId.ToString(); var objectId = doc["_id"].AsObjectId.ToString();
context.ids = new string[] { objectId }; context.ids = new string[] { objectId };
return 1; return 1;

View File

@@ -18,7 +18,14 @@ namespace MongoDb.Ado.data
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{ {
var documents = ParseJsonArray(json); var documents = ParseJsonArray(json);
await collection.InsertManyAsync(documents,null,token); if (context.IsAnyServerSession)
{
await collection.InsertManyAsync(context.ServerSession,documents, null, token);
}
else
{
await collection.InsertManyAsync(documents, null, token);
}
var objectIds = documents.Select(it => it["_id"].AsObjectId.ToString()).ToArray(); var objectIds = documents.Select(it => it["_id"].AsObjectId.ToString()).ToArray();
context.ids = objectIds; context.ids = objectIds;
return documents.Count; return documents.Count;

View File

@@ -18,8 +18,16 @@ namespace MongoDb.Ado.data
var doc = BsonDocument.Parse(json); var doc = BsonDocument.Parse(json);
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var update = doc["update"].AsBsonDocument; var update = doc["update"].AsBsonDocument;
var result =await collection.UpdateOneAsync(filter, update,null,token); if (context.IsAnyServerSession)
return (int)result.ModifiedCount; {
var result = await collection.UpdateOneAsync(context.ServerSession,filter, update, null, token);
return (int)result.ModifiedCount;
}
else
{
var result = await collection.UpdateOneAsync(filter, update, null, token);
return (int)result.ModifiedCount;
}
} }
} }
} }

View File

@@ -22,8 +22,16 @@ namespace MongoDb.Ado.data
{ {
var filter = doc["filter"].AsBsonDocument; var filter = doc["filter"].AsBsonDocument;
var update = doc["update"].AsBsonDocument; var update = doc["update"].AsBsonDocument;
var result =await collection.UpdateManyAsync(filter, update,null,token); if (context.IsAnyServerSession)
total += (int)result.ModifiedCount; {
var result = await collection.UpdateManyAsync(context.ServerSession,filter, update, null, token);
total += (int)result.ModifiedCount;
}
else
{
var result = await collection.UpdateManyAsync(filter, update, null, token);
total += (int)result.ModifiedCount;
}
} }
return total; return total;
} }

View File

@@ -1,5 +1,7 @@
using System; using MongoDB.Driver;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common;
using System.Text; using System.Text;
namespace MongoDb.Ado.data namespace MongoDb.Ado.data
@@ -7,5 +9,9 @@ namespace MongoDb.Ado.data
public class HandlerContext public class HandlerContext
{ {
public string[] ids { get; set; } public string[] ids { get; set; }
public DbConnection Connection { get; set; }
public MongoDbConnection MongoDbConnection { get { return Connection as MongoDbConnection; } }
public IClientSessionHandle ServerSession { get { return this.MongoDbConnection?.iClientSessionHandle; } }
public bool IsAnyServerSession { get { return ServerSession != null; } }
} }
} }

View File

@@ -61,7 +61,7 @@ namespace MongoDb.Ado.data
{ {
var (operation, collectionName, json) = ParseCommand(_commandText); var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName); var collection = GetCollection(collectionName);
var context = new HandlerContext(); var context = new HandlerContext() { Connection = this.Connection };
var result= ExecuteHandlerFactory.Handler(operation, json, collection, context); var result= ExecuteHandlerFactory.Handler(operation, json, collection, context);
((MongoDbConnection)this.Connection).ObjectIds = context.ids; ((MongoDbConnection)this.Connection).ObjectIds = context.ids;
return result; return result;
@@ -83,7 +83,7 @@ namespace MongoDb.Ado.data
{ {
var (operation, collectionName, json) = ParseCommand(_commandText); var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName); var collection = GetCollection(collectionName);
var context = new HandlerContext(); var context = new HandlerContext() { Connection = this.Connection};
var result= await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken,context); var result= await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken,context);
((MongoDbConnection)this.Connection).ObjectIds = context.ids; ((MongoDbConnection)this.Connection).ObjectIds = context.ids;
return result; return result;

View File

@@ -12,7 +12,7 @@ namespace MongoDb.Ado.data
{ {
private static readonly Dictionary<string, MongoClient> _clientCache = new Dictionary<string, MongoClient>(StringComparer.OrdinalIgnoreCase); private static readonly Dictionary<string, MongoClient> _clientCache = new Dictionary<string, MongoClient>(StringComparer.OrdinalIgnoreCase);
private static readonly object _lock = new object(); private static readonly object _lock = new object();
public IClientSessionHandle iClientSessionHandle;
private string _originalConnectionString; private string _originalConnectionString;
private IMongoDatabase _database; private IMongoDatabase _database;
private string _databaseName; private string _databaseName;

View File

@@ -15,7 +15,17 @@ namespace SqlSugar.MongoDb
{ {
public partial class MongoDbProvider : AdoProvider public partial class MongoDbProvider : AdoProvider
{ {
IClientSessionHandle iClientSessionHandle; IClientSessionHandle iClientSessionHandle
{
get
{
return (this.Connection as MongoDbConnection).iClientSessionHandle;
}
set
{
(this.Connection as MongoDbConnection).iClientSessionHandle = value;
}
}
public MongoDbProvider() public MongoDbProvider()
{ {
if (StaticConfig.AppContext_ConvertInfinityDateTime == false) if (StaticConfig.AppContext_ConvertInfinityDateTime == false)