mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 10:38:09 +08:00
Update mongodb
This commit is contained in:
@@ -25,9 +25,17 @@ namespace MongoDb.Ado.data
|
|||||||
bulkOps.Add(op);
|
bulkOps.Add(op);
|
||||||
}
|
}
|
||||||
if (bulkOps.Count == 0) return 0;
|
if (bulkOps.Count == 0) return 0;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = collection.BulkWrite(context.ServerSession,bulkOps);
|
||||||
|
return (int)result.ModifiedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = collection.BulkWrite(bulkOps);
|
var result = collection.BulkWrite(bulkOps);
|
||||||
return (int)result.ModifiedCount;
|
return (int)result.ModifiedCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private List<BsonDocument> ParseJsonArray(string json)
|
private List<BsonDocument> ParseJsonArray(string json)
|
||||||
{
|
{
|
||||||
|
@@ -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;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = collection.DeleteOne(context.ServerSession,filter);
|
||||||
|
return (int)result.DeletedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = collection.DeleteOne(filter);
|
var result = collection.DeleteOne(filter);
|
||||||
return (int)result.DeletedCount;
|
return (int)result.DeletedCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@@ -18,9 +18,17 @@ namespace MongoDb.Ado.data
|
|||||||
foreach (var doc in documents)
|
foreach (var doc in documents)
|
||||||
{
|
{
|
||||||
var filter = doc["filter"].AsBsonDocument;
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = collection.DeleteMany(context.ServerSession,filter);
|
||||||
|
total += (int)result.DeletedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = collection.DeleteMany(filter);
|
var result = collection.DeleteMany(filter);
|
||||||
total += (int)result.DeletedCount;
|
total += (int)result.DeletedCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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);
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
collection.InsertOne(context.ServerSession,doc);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
collection.InsertOne(doc);
|
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;
|
||||||
|
@@ -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);
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
collection.InsertMany(context.ServerSession,documents);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
collection.InsertMany(documents);
|
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;
|
||||||
|
@@ -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;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = collection.UpdateOne(context.ServerSession,filter, update);
|
||||||
|
return (int)result.ModifiedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = collection.UpdateOne(filter, update);
|
var result = collection.UpdateOne(filter, update);
|
||||||
return (int)result.ModifiedCount;
|
return (int)result.ModifiedCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -19,9 +19,17 @@ namespace MongoDb.Ado.data
|
|||||||
{
|
{
|
||||||
var filter = doc["filter"].AsBsonDocument;
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
var update = doc["update"].AsBsonDocument;
|
var update = doc["update"].AsBsonDocument;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = collection.UpdateMany(context.ServerSession,filter, update);
|
||||||
|
total += (int)result.ModifiedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = collection.UpdateMany(filter, update);
|
var result = collection.UpdateMany(filter, update);
|
||||||
total += (int)result.ModifiedCount;
|
total += (int)result.ModifiedCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -28,9 +28,17 @@ namespace MongoDb.Ado.data
|
|||||||
bulkOps.Add(op);
|
bulkOps.Add(op);
|
||||||
}
|
}
|
||||||
if (bulkOps.Count == 0) return 0;
|
if (bulkOps.Count == 0) return 0;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = await collection.BulkWriteAsync(context.ServerSession,bulkOps);
|
||||||
|
return (int)result.ModifiedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = await collection.BulkWriteAsync(bulkOps);
|
var result = await collection.BulkWriteAsync(bulkOps);
|
||||||
return (int)result.ModifiedCount;
|
return (int)result.ModifiedCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private List<BsonDocument> ParseJsonArray(string json)
|
private List<BsonDocument> ParseJsonArray(string json)
|
||||||
{
|
{
|
||||||
|
@@ -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;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = await collection.DeleteOneAsync(context.ServerSession,filter,null,token);
|
||||||
|
return (int)result.DeletedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = await collection.DeleteOneAsync(filter, token);
|
var result = await collection.DeleteOneAsync(filter, token);
|
||||||
return (int)result.DeletedCount;
|
return (int)result.DeletedCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@@ -21,9 +21,17 @@ namespace MongoDb.Ado.data
|
|||||||
foreach (var doc in documents)
|
foreach (var doc in documents)
|
||||||
{
|
{
|
||||||
var filter = doc["filter"].AsBsonDocument;
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
var result = await collection.DeleteManyAsync(context.ServerSession,filter,null, token);
|
||||||
|
total += (int)result.DeletedCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var result = await collection.DeleteManyAsync(filter, token);
|
var result = await collection.DeleteManyAsync(filter, token);
|
||||||
total += (int)result.DeletedCount;
|
total += (int)result.DeletedCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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);
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
await collection.InsertOneAsync(context.ServerSession,doc, null, token);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
await collection.InsertOneAsync(doc, null, token);
|
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;
|
||||||
|
@@ -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);
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
await collection.InsertManyAsync(context.ServerSession,documents, null, token);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
await collection.InsertManyAsync(documents, null, token);
|
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;
|
||||||
|
@@ -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;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
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);
|
var result = await collection.UpdateOneAsync(filter, update, null, token);
|
||||||
return (int)result.ModifiedCount;
|
return (int)result.ModifiedCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@@ -22,9 +22,17 @@ namespace MongoDb.Ado.data
|
|||||||
{
|
{
|
||||||
var filter = doc["filter"].AsBsonDocument;
|
var filter = doc["filter"].AsBsonDocument;
|
||||||
var update = doc["update"].AsBsonDocument;
|
var update = doc["update"].AsBsonDocument;
|
||||||
|
if (context.IsAnyServerSession)
|
||||||
|
{
|
||||||
|
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);
|
var result = await collection.UpdateManyAsync(filter, update, null, token);
|
||||||
total += (int)result.ModifiedCount;
|
total += (int)result.ModifiedCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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; } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
@@ -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;
|
||||||
|
@@ -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)
|
||||||
|
Reference in New Issue
Block a user