2025-06-25 14:33:11 +08:00
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using SqlSugar.MongoDb;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MongoDbTest
|
|
|
|
|
{
|
|
|
|
|
internal class QueryLeftJoin
|
|
|
|
|
{
|
|
|
|
|
internal static void Init()
|
|
|
|
|
{
|
|
|
|
|
var db = DbHelper.GetNewDb();
|
|
|
|
|
db.DbMaintenance.TruncateTable<School, School>();
|
|
|
|
|
// 添加学校数据
|
|
|
|
|
var school = new School { Name = "TestSchool" };
|
|
|
|
|
var ids=db.Insertable(school).ExecuteReturnPkList<string>();
|
|
|
|
|
// 添加学生数据,SchoolId 关联学校
|
|
|
|
|
var student = new Student { Name = "TestStudent", SchoolId = ids.Last() };
|
|
|
|
|
db.Insertable(student).ExecuteCommand();
|
|
|
|
|
// 添加学生数据,SchoolId 关联学校并且没有学校
|
2025-06-26 14:33:33 +08:00
|
|
|
|
var student2 = new Student { Name = "TestStudentNoSchool", SchoolId =
|
2025-06-25 14:33:11 +08:00
|
|
|
|
ObjectId.GenerateNewId().ToString()
|
|
|
|
|
};
|
|
|
|
|
db.Insertable(student2).ExecuteCommand();
|
|
|
|
|
|
2025-06-26 14:33:33 +08:00
|
|
|
|
|
|
|
|
|
if (db.Queryable<Student>().First(it => it.SchoolId == ids.Last()).Name != "TestStudent") Cases.ThrowUnitError();
|
|
|
|
|
db.Updateable(db.Queryable<Student>().First(it => it.SchoolId == ids.Last())).ExecuteCommand();
|
|
|
|
|
if (db.Queryable<Student>().First(it => it.SchoolId == ids.Last()).Name != "TestStudent") Cases.ThrowUnitError();
|
|
|
|
|
|
2025-06-27 16:22:17 +08:00
|
|
|
|
var adoTest= db.Ado.GetDataTable(@"aggregate UnitStudent123131 [
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: ""UnitSchool123131"",
|
|
|
|
|
localField: ""SchoolId"",
|
|
|
|
|
foreignField: ""_id"",
|
|
|
|
|
as: ""y""
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$unwind: {
|
|
|
|
|
path: ""$y"",
|
|
|
|
|
preserveNullAndEmptyArrays: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
StudentName: ""$Name"",
|
|
|
|
|
SchoolName: {
|
|
|
|
|
$ifNull: [""$y.Name"", null]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]");
|
|
|
|
|
|
2025-06-28 09:10:17 +08:00
|
|
|
|
var list = db.Queryable<Student>()
|
|
|
|
|
.LeftJoin<School>((x, y) => x.SchoolId == y.Id)
|
|
|
|
|
.Select((x, y) => new
|
|
|
|
|
{
|
|
|
|
|
StudentName = x.Name,
|
|
|
|
|
SchoolName = y.Name
|
|
|
|
|
}).ToList();
|
2025-06-28 09:18:58 +08:00
|
|
|
|
|
|
|
|
|
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();
|
2025-06-28 09:55:12 +08:00
|
|
|
|
|
|
|
|
|
var list3 = db.Queryable<Student>()
|
|
|
|
|
.LeftJoin<School>((x, y) => x.SchoolId == y.Id)
|
|
|
|
|
.Where((x,y)=>x.Name=="jack")
|
|
|
|
|
.Select((x, y) => new
|
|
|
|
|
{
|
|
|
|
|
StudentName = x.Name,
|
|
|
|
|
SchoolName = y.Name
|
|
|
|
|
}).ToList();
|
|
|
|
|
if (list3.Any(s => s.SchoolName ==null&&s.StudentName=="jack")==false) Cases.ThrowUnitError();
|
|
|
|
|
|
|
|
|
|
var list4 = db.Queryable<Student>()
|
|
|
|
|
.LeftJoin<School>((x, y) => x.SchoolId == y.Id)
|
|
|
|
|
.Where((x, y) => x.Name == "jack"||y.Name== "TestSchool")
|
|
|
|
|
.Select((x, y) => new
|
|
|
|
|
{
|
|
|
|
|
StudentName = x.Name,
|
|
|
|
|
SchoolName = y.Name
|
|
|
|
|
}).ToList();
|
|
|
|
|
if(list4.Count!=2) Cases.ThrowUnitError();
|
|
|
|
|
if (list4.Any(s=>s.SchoolName== "TestSchool") ==false) Cases.ThrowUnitError();
|
|
|
|
|
if (list4.Any(s => s.SchoolName == null||s.StudentName== "jack") == false) Cases.ThrowUnitError();
|
2025-06-25 14:33:11 +08:00
|
|
|
|
}
|
|
|
|
|
[SqlSugar.SugarTable("UnitStudent123131")]
|
|
|
|
|
public class Student : MongoDbBase
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
2025-06-26 14:33:33 +08:00
|
|
|
|
[SqlSugar.SugarColumn(ColumnDataType =nameof(ObjectId))]
|
2025-06-25 14:33:11 +08:00
|
|
|
|
public string SchoolId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
[SqlSugar.SugarTable("UnitSchool123131")]
|
|
|
|
|
public class School : MongoDbBase
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|