Update mongodb

This commit is contained in:
sunkaixuan
2025-06-28 09:18:58 +08:00
parent c8cb8364b7
commit ba92e9458f
2 changed files with 43 additions and 13 deletions

View File

@@ -64,6 +64,20 @@ namespace MongoDbTest
StudentName = x.Name,
SchoolName = y.Name
}).ToList();
if (list.Count != 3) Cases.ThrowUnitError();
if (list.Any(s=>s.SchoolName== "TestSchool") ==false) Cases.ThrowUnitError();
if (list.Any(s => s.StudentName == "jack") == false) Cases.ThrowUnitError();
var list2 = db.Queryable<Student>()
.InnerJoin<School>((x, y) => x.SchoolId == y.Id)
.Select((x, y) => new
{
StudentName = x.Name,
SchoolName = y.Name
}).ToList();
if (list2.Count != 1) Cases.ThrowUnitError();
if (list2.Any(s => s.SchoolName == "TestSchool") == false) Cases.ThrowUnitError();
}
[SqlSugar.SugarTable("UnitStudent123131")]
public class Student : MongoDbBase

View File

@@ -215,11 +215,27 @@ namespace SqlSugar.MongoDb
operations.Add(lookupDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
// $unwind
var unwindDoc = new BsonDocument("$unwind", new BsonDocument
BsonValue unwindDoc = null;
if (item.JoinType == JoinType.Left)
{
unwindDoc=new BsonDocument("$unwind", new BsonDocument
{
{ "path", $"${asName}" },
{ "preserveNullAndEmptyArrays", true }
});
}
else if(item.JoinType==JoinType.Inner)
{
// InnerJoin: 不保留空数组和null
unwindDoc = new BsonDocument("$unwind", new BsonDocument
{
{ "path", $"${asName}" }
});
}
else
{
throw new Exception(" No Support "+item.JoinType);
}
operations.Add(unwindDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
}
}