Update mongodb

This commit is contained in:
sunkaixuan
2025-06-28 09:10:17 +08:00
parent 9d49280e65
commit c8cb8364b7
2 changed files with 44 additions and 14 deletions

View File

@@ -57,13 +57,13 @@ namespace MongoDbTest
}
]");
//var list = db.Queryable<Student>()
// .LeftJoin<School>((x, y) => x.SchoolId == y.Id)
// .Select((x, y) => new
// {
// StudentName = x.Name,
// SchoolName = y.Name
// }).ToList();
var list = db.Queryable<Student>()
.LeftJoin<School>((x, y) => x.SchoolId == y.Id)
.Select((x, y) => new
{
StudentName = x.Name,
SchoolName = y.Name
}).ToList();
}
[SqlSugar.SugarTable("UnitStudent123131")]
public class Student : MongoDbBase

View File

@@ -190,21 +190,51 @@ namespace SqlSugar.MongoDb
}
private void ProcessJoinInfoConditions(List<string> operations)
{
foreach (var item in this.JoinQueryInfos)
{
// 解析 JoinWhere JSON假设格式为 { "SchoolId" : { "$eq" : "_id" } }
var joinWhereDoc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(item.JoinWhere);
// 只支持单字段等值连接
var localField = joinWhereDoc.GetElement(0).Name;
var eqObj = joinWhereDoc[localField].AsBsonDocument;
var foreignField = eqObj.GetElement(0).Value.AsString;
// from: 目标集合名(假设 JoinQueryInfo 有 TableName/ShortName 字段,需根据实际情况调整)
// as: 关联后的别名(假设 JoinQueryInfo 有 ShortName 字段)
string from = item.TableName ?? item.ShortName ?? "Unknown";
string asName = item.ShortName ?? "y";
// $lookup
var lookupDoc = new BsonDocument("$lookup", new BsonDocument
{
{ "from", from },
{ "localField", localField },
{ "foreignField", foreignField },
{ "as", asName }
});
operations.Add(lookupDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
// $unwind
var unwindDoc = new BsonDocument("$unwind", new BsonDocument
{
{ "path", $"${asName}" },
{ "preserveNullAndEmptyArrays", true }
});
operations.Add(unwindDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
}
}
#endregion
#region Get SQL Partial
public override string GetTableNameString
{
get
{
if (this.TableShortName != null && this.Context.CurrentConnectionConfig?.MoreSettings?.PgSqlIsAutoToLower == false)
{
this.TableShortName = Builder.GetTranslationColumnName(this.TableShortName);
}
return base.GetTableNameString;
if (this.AsTables.Any())
return this.AsTables.FirstOrDefault().Value;
else
return this.Context.EntityMaintenance.GetEntityInfo(this.EntityType).DbTableName;
}
}
public override bool IsComplexModel(string sql)