Update mongodb

This commit is contained in:
sunkaixuan
2025-08-24 15:38:20 +08:00
parent 25be5cb662
commit 1ed56dc26b

View File

@@ -78,40 +78,42 @@ namespace MongoDbTest
{
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Book = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Books = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
var data1 = db.Queryable<Student>().ToList();
if (data1.First().Book.Count != 1) Cases.ThrowUnitError();
if (data1.First().Book.First().Price != 21) Cases.ThrowUnitError();
data1.First().Book.First().Price = 100;
if (data1.First().Books.Count != 1) Cases.ThrowUnitError();
if (data1.First().Books.First().Price != 21) Cases.ThrowUnitError();
data1.First().Books.First().Price = 100;
db.Updateable(data1).ExecuteCommand();
var data2 = db.Queryable<Student>().ToList();
if (data2.First().Book.First().Price != 100) Cases.ThrowUnitError();
if (data2.First().Books.First().Price != 100) Cases.ThrowUnitError();
var exp = Expressionable.Create<Student>().ToExpression();
var data3 = db.Queryable<Student>().Where(exp).ToList();
db.Insertable(new Student() { Age = 1, Name = "haha", SchoolId = "1", Book = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
var data4 = db.Queryable<Student>().Where(it => it.Book.Any(s => s.Price == 21)).ToList();
if (data4.Count != 1 || data4.First().Book.First().Price != 21) Cases.ThrowUnitError();
var data5 = db.Queryable<Student>().Where(it => it.Book.Any(s => s.Price == 21 || s.Price == 100)).ToList();
db.Insertable(new Student() { Age = 1, Name = "haha", SchoolId = "1", Books = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
var data4 = db.Queryable<Student>().Where(it => it.Books.Any(s => s.Price == 21)).ToList();
if (data4.Count != 1 || data4.First().Books.First().Price != 21) Cases.ThrowUnitError();
var data5 = db.Queryable<Student>().Where(it => it.Books.Any(s => s.Price == 21 || s.Price == 100)).ToList();
db.DbMaintenance.TruncateTable<Student>();
var id = ObjectId.GenerateNewId() + "";
db.Insertable(new Student() { Age = 1, Name = "a", SchoolId = "1", Book = new List<Book>() { new Book() { SId = id, CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
db.Insertable(new Student() { Age = 1, Name = "b", SchoolId = "1", Book = new List<Book>() { new Book() { SId = id, CreateTime = DateTime.Now, Price = 100 } } }).ExecuteCommand();
db.Insertable(new Student() { Age = 1, Name = "c", SchoolId = "1", Book = new List<Book>() { new Book() { SId = ObjectId.GenerateNewId() + "", CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
var data6 = db.Queryable<Student>().Where(it => it.Book.Any(s => s.Price == 21 && s.SId == id)).ToList();
db.Insertable(new Student() { Age = 1, Name = "a", SchoolId = "1", Books = new List<Book>() { new Book() { SId = id, CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
db.Insertable(new Student() { Age = 1, Name = "b", SchoolId = "1", Books = new List<Book>() { new Book() { SId = id, CreateTime = DateTime.Now, Price = 100 } } }).ExecuteCommand();
db.Insertable(new Student() { Age = 1, Name = "c", SchoolId = "1", Books = new List<Book>() { new Book() { SId = ObjectId.GenerateNewId() + "", CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
var data6 = db.Queryable<Student>().Where(it => it.Books.Any(s => s.Price == 21 && s.SId == id)).ToList();
if (data6.Count != 1 || data6.First().Name != "a") Cases.ThrowUnitError();
db.Insertable(new Student() { Age = 99, Name = "price=age", SchoolId = "1", Book = new List<Book>() { new Book() { SId = ObjectId.GenerateNewId() + "", CreateTime = DateTime.Now, Price = 99 } } }).ExecuteCommand();
var data7 = db.Queryable<Student>().Where(it => it.Book.Any(s => s.Price == it.Age)).ToList();
var data8 = db.Queryable<Student>().Where(it => it.Book.Any(s => it.Age == s.Price)).ToList();
db.Insertable(new Student() { Age = 99, Name = "price=age", SchoolId = "1", Books = new List<Book>() { new Book() { SId = ObjectId.GenerateNewId() + "", CreateTime = DateTime.Now, Price = 99 } } }).ExecuteCommand();
var data7 = db.Queryable<Student>().Where(it => it.Books.Any(s => s.Price == it.Age)).ToList();
var data8 = db.Queryable<Student>().Where(it => it.Books.Any(s => it.Age == s.Price)).ToList();
if (data7.Count != 1 || data8.Count != 1) Cases.ThrowUnitError();
if (data7.FirstOrDefault().Name != "price=age" || data8.FirstOrDefault().Name != "price=age") Cases.ThrowUnitError();
var data9 = db.Queryable<Student>().Where(it => it.Book.Any()).ToList();
db.Insertable(new Student() { Age = 1000, Name = "call", SchoolId = "1", Book = new List<Book>() { new Book() { NumStr = "1", book=new List<Book>() { new Book() { } } } } }).ExecuteCommand();
var data12 = db.Queryable<Student>().Where(it => it.Book.Any(s => s.NumStr == "1")).ToList();
var data13 = db.Queryable<Student>().Where(it => it.Book.Any(s => SqlFunc.ToInt32(s.NumStr) ==1)).ToList();
var data9 = db.Queryable<Student>().Where(it => it.Books.Any()).ToList();
db.Insertable(new Student() { Age = 1000, Name = "call", SchoolId = "1", Books = new List<Book>() { new Book() { NumStr = "1", BookChildList=new List<Book>() { new Book() { } } } } }).ExecuteCommand();
var data12 = db.Queryable<Student>().Where(it => it.Books.Any(s => s.NumStr == "1")).ToList();
var data13 = db.Queryable<Student>().Where(it => it.Books.Any(s => SqlFunc.ToInt32(s.NumStr) ==1)).ToList();
if (data13.First().Name != "call") Cases.ThrowUnitError();
db.Insertable(new Student() { Age = 1, Name = "adddays1", SchoolId = "1", Book = new List<Book>() { new Book() { TimeStr=DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") } } }).ExecuteCommand();
var data14 = db.Queryable<Student>().Where(it => it.Book.Any(s => Convert.ToDateTime(s.TimeStr) > DateTime.Now)).ToList();
db.Insertable(new Student() { Age = 1, Name = "adddays1", SchoolId = "1", Books = new List<Book>() { new Book() { TimeStr=DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") } } }).ExecuteCommand();
var data14 = db.Queryable<Student>().Where(it => it.Books.Any(s => Convert.ToDateTime(s.TimeStr) > DateTime.Now)).ToList();
if(data14.First().Name!= "adddays1") Cases.ThrowUnitError();
var data15 = db.Queryable<Student>().Where(it => it.Books.Any(s =>s.BookChildList.Any())).ToList();
if (data15.Any(it=>it.Books.Any()==false)) Cases.ThrowUnitError();
}
[SqlSugar.SugarTable("UnitStudentdfsds3zzz1")]
@@ -126,7 +128,7 @@ namespace MongoDbTest
public DateTime CreateDateTime { get; set; }
[SqlSugar.SugarColumn(IsJson = true)]
public List<Book> Book { get; set; }
public List<Book> Books { get; set; }
}
public class IdsModel
{
@@ -147,7 +149,7 @@ namespace MongoDbTest
[SqlSugar.SugarColumn(ColumnDataType = nameof(ObjectId))]
public string SId { get; set; }
public List<Book> book { get; set; }
public List<Book> BookChildList { get; set; }
}
}