Files
SqlSugar/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteNonQueryItems/InsertHandler.cs

30 lines
870 B
C#
Raw Normal View History

2025-05-02 07:54:16 +08:00
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
namespace MongoDb.Ado.data
{
public class InsertHandler : IMongoOperationHandler
{
2025-05-04 18:39:11 +08:00
public HandlerContext context { get; set; }
2025-05-02 16:55:48 +08:00
public string operation { get; set; }
2025-05-02 07:54:16 +08:00
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);
2025-07-11 17:20:42 +08:00
if (context.IsAnyServerSession)
{
collection.InsertOne(context.ServerSession,doc);
}
else
{
collection.InsertOne(doc);
}
2025-07-18 14:11:05 +08:00
var objectId = doc["_id"].IsObjectId ? doc["_id"].AsObjectId.ToString() : doc["_id"].ToString();
2025-07-09 12:07:56 +08:00
context.ids = new string[] { objectId };
2025-05-02 07:54:16 +08:00
return 1;
}
}
}