Update mongodb

This commit is contained in:
sunkaixuan
2025-11-21 16:42:14 +08:00
parent 14525e8c2b
commit 0d20cc8c1c

View File

@@ -2,6 +2,7 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.IO; using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using NetTaste; using NetTaste;
using System; using System;
using System.Collections; using System.Collections;
@@ -305,8 +306,13 @@ namespace SqlSugar.MongoDb
foreach (var group in grouped) foreach (var group in grouped)
{ {
BsonDocument doc = new BsonDocument(); BsonDocument doc = new BsonDocument();
var existsId = false;
foreach (var col in group) foreach (var col in group)
{ {
if (col.DbColumnName == "_id")
{
existsId = true;
}
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等 // 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
if (col.IsJson == true) if (col.IsJson == true)
{ {
@@ -330,7 +336,10 @@ namespace SqlSugar.MongoDb
doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value); doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value);
} }
} }
if (existsId == false)
{
doc["_id"] = ObjectId.GenerateNewId();
}
// 转为 JSON 字符串(标准 MongoDB shell 格式) // 转为 JSON 字符串(标准 MongoDB shell 格式)
string json = doc.ToJson(UtilMethods.GetJsonWriterSettings()); string json = doc.ToJson(UtilMethods.GetJsonWriterSettings());