From 78c2f1840dd2611cf1c6ec0491e716ea98b2179b Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 4 Aug 2025 19:33:38 +0800 Subject: [PATCH] Add demo --- .../MongoDbTest/UnitTest/Cases.cs | 1 + .../MongoDbTest/UnitTest/QueryJson2.cs | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson2.cs diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs index aa33f4c50..49190aa4b 100644 --- a/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs @@ -16,6 +16,7 @@ namespace MongoDbTest QuerySelect.Init(); QuerySelect2.Init(); QueryJson.Init(); + QueryJson2.Init(); QueryLeftJoin.Init(); QueryLeftJoin2.Init(); QueryLeftJoin3.Init(); diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson2.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson2.cs new file mode 100644 index 000000000..103985adb --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson2.cs @@ -0,0 +1,54 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using SqlSugar.MongoDb; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MongoDbTest +{ + public class QueryJson2 + { + internal static void Init() + { + var db = DbHelper.GetNewDb(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new Student() + { + Age = 1, + Book = new Book() { SchoolId = ObjectId.GenerateNewId().ToString() } + }).ExecuteCommand(); + var data = db.Queryable().First(); + var list=db.Queryable().Where(s => s.Book.SchoolId == data.Book.SchoolId).ToList(); + if (list.Any() == false) Cases.ThrowUnitError(); + } + + [SqlSugar.SugarTable("UnitStudentdddd1")] + public class Student : MongoDbBase + { + public string Name { get; set; } + + public string SchoolId { get; set; } + + public int Age { get; set; } + + public DateTime CreateDateTime { get; set; } + + [SqlSugar.SugarColumn(IsJson = true)] + public Book Book { get; set; } + } + + public class Book + { + [BsonRepresentation(BsonType.ObjectId)]//比普通类多个序列化ObjectId + [SqlSugar.SugarColumn(ColumnDataType = nameof(ObjectId))] + public string SchoolId { get; set; } + + public decimal BookId { get; set; } + } + } + +}