mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 18:22:23 +08:00
Update mongodb
This commit is contained in:
@@ -25,8 +25,16 @@ namespace MongoDb.Ado.data
|
||||
bulkOps.Add(op);
|
||||
}
|
||||
if (bulkOps.Count == 0) return 0;
|
||||
var result = collection.BulkWrite(bulkOps);
|
||||
return (int)result.ModifiedCount;
|
||||
if (context.IsAnyServerSession)
|
||||
{
|
||||
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)
|
||||
|
@@ -15,8 +15,16 @@ namespace MongoDb.Ado.data
|
||||
{
|
||||
var doc = BsonDocument.Parse(json);
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var result = collection.DeleteOne(filter);
|
||||
return (int)result.DeletedCount;
|
||||
if (context.IsAnyServerSession)
|
||||
{
|
||||
var result = collection.DeleteOne(context.ServerSession,filter);
|
||||
return (int)result.DeletedCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = collection.DeleteOne(filter);
|
||||
return (int)result.DeletedCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,8 +18,16 @@ namespace MongoDb.Ado.data
|
||||
foreach (var doc in documents)
|
||||
{
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var result = collection.DeleteMany(filter);
|
||||
total += (int)result.DeletedCount;
|
||||
if (context.IsAnyServerSession)
|
||||
{
|
||||
var result = collection.DeleteMany(context.ServerSession,filter);
|
||||
total += (int)result.DeletedCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = collection.DeleteMany(filter);
|
||||
total += (int)result.DeletedCount;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
@@ -13,7 +13,14 @@ namespace MongoDb.Ado.data
|
||||
public int Handle(IMongoCollection<BsonDocument> collection, string 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();
|
||||
context.ids = new string[] { objectId };
|
||||
return 1;
|
||||
|
@@ -15,7 +15,14 @@ namespace MongoDb.Ado.data
|
||||
public int Handle(IMongoCollection<BsonDocument> collection, string 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();
|
||||
context.ids = objectIds;
|
||||
return documents.Count;
|
||||
|
@@ -15,8 +15,17 @@ namespace MongoDb.Ado.data
|
||||
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;
|
||||
if (context.IsAnyServerSession)
|
||||
{
|
||||
var result = collection.UpdateOne(context.ServerSession,filter, update);
|
||||
return (int)result.ModifiedCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = collection.UpdateOne(filter, update);
|
||||
return (int)result.ModifiedCount;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,8 +19,16 @@ namespace MongoDb.Ado.data
|
||||
{
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var update = doc["update"].AsBsonDocument;
|
||||
var result = collection.UpdateMany(filter, update);
|
||||
total += (int)result.ModifiedCount;
|
||||
if (context.IsAnyServerSession)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@@ -28,8 +28,16 @@ namespace MongoDb.Ado.data
|
||||
bulkOps.Add(op);
|
||||
}
|
||||
if (bulkOps.Count == 0) return 0;
|
||||
var result =await collection.BulkWriteAsync(bulkOps);
|
||||
return (int)result.ModifiedCount;
|
||||
if (context.IsAnyServerSession)
|
||||
{
|
||||
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)
|
||||
|
@@ -17,8 +17,16 @@ namespace MongoDb.Ado.data
|
||||
{
|
||||
var doc = BsonDocument.Parse(json);
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var result =await collection.DeleteOneAsync(filter,token);
|
||||
return (int)result.DeletedCount;
|
||||
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);
|
||||
return (int)result.DeletedCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -21,8 +21,16 @@ namespace MongoDb.Ado.data
|
||||
foreach (var doc in documents)
|
||||
{
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var result =await collection.DeleteManyAsync(filter,token);
|
||||
total += (int)result.DeletedCount;
|
||||
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);
|
||||
total += (int)result.DeletedCount;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
@@ -16,7 +16,14 @@ namespace MongoDb.Ado.data
|
||||
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string 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();
|
||||
context.ids = new string[] { objectId };
|
||||
return 1;
|
||||
|
@@ -18,7 +18,14 @@ namespace MongoDb.Ado.data
|
||||
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string 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();
|
||||
context.ids = objectIds;
|
||||
return documents.Count;
|
||||
|
@@ -18,8 +18,16 @@ namespace MongoDb.Ado.data
|
||||
var doc = BsonDocument.Parse(json);
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var update = doc["update"].AsBsonDocument;
|
||||
var result =await collection.UpdateOneAsync(filter, update,null,token);
|
||||
return (int)result.ModifiedCount;
|
||||
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);
|
||||
return (int)result.ModifiedCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -22,8 +22,16 @@ namespace MongoDb.Ado.data
|
||||
{
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var update = doc["update"].AsBsonDocument;
|
||||
var result =await collection.UpdateManyAsync(filter, update,null,token);
|
||||
total += (int)result.ModifiedCount;
|
||||
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);
|
||||
total += (int)result.ModifiedCount;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
@@ -7,5 +9,9 @@ namespace MongoDb.Ado.data
|
||||
public class HandlerContext
|
||||
{
|
||||
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 collection = GetCollection(collectionName);
|
||||
var context = new HandlerContext();
|
||||
var context = new HandlerContext() { Connection = this.Connection };
|
||||
var result= ExecuteHandlerFactory.Handler(operation, json, collection, context);
|
||||
((MongoDbConnection)this.Connection).ObjectIds = context.ids;
|
||||
return result;
|
||||
@@ -83,7 +83,7 @@ namespace MongoDb.Ado.data
|
||||
{
|
||||
var (operation, collectionName, json) = ParseCommand(_commandText);
|
||||
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);
|
||||
((MongoDbConnection)this.Connection).ObjectIds = context.ids;
|
||||
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 object _lock = new object();
|
||||
|
||||
public IClientSessionHandle iClientSessionHandle;
|
||||
private string _originalConnectionString;
|
||||
private IMongoDatabase _database;
|
||||
private string _databaseName;
|
||||
|
@@ -15,7 +15,17 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
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()
|
||||
{
|
||||
if (StaticConfig.AppContext_ConvertInfinityDateTime == false)
|
||||
|
Reference in New Issue
Block a user