Files
SqlSugar/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson2.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2025-08-04 19:33:38 +08:00
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<Student>();
db.DbMaintenance.TruncateTable<Student>();
2025-08-08 13:17:30 +08:00
var dt = DateTime.Now;
2025-08-04 19:33:38 +08:00
db.Insertable(new Student()
{
Age = 1,
2025-08-08 13:17:30 +08:00
Book = new Book() { SchoolId = ObjectId.GenerateNewId().ToString(),DateTime=dt}
2025-08-04 19:33:38 +08:00
}).ExecuteCommand();
var data = db.Queryable<Student>().First();
var list=db.Queryable<Student>().Where(s => s.Book.SchoolId == data.Book.SchoolId).ToList();
2025-08-05 13:06:33 +08:00
if (list.Any() == false) Cases.ThrowUnitError();
var ids = new List<string>() { data.Book.SchoolId};
var list2 = db.Queryable<Student>().Where(s => ids.Contains( s.Book.SchoolId ) ).ToList();
if (list2.Any() == false) Cases.ThrowUnitError();
2025-08-08 13:17:30 +08:00
if(data.Book.DateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") != dt.ToString("yyyy-MM-dd HH:mm:ss.fff")) Cases.ThrowUnitError();
2025-08-04 19:33:38 +08:00
}
[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; }
2025-08-08 13:17:30 +08:00
public DateTime DateTime { get; set; }
2025-08-04 19:33:38 +08:00
}
}
}