Update mongodb

This commit is contained in:
sunkaixuan
2025-08-23 20:29:13 +08:00
parent e8ab9fde99
commit 4deb4dc356

View File

@@ -15,42 +15,26 @@ namespace MongoDbTest
internal static void Init() internal static void Init()
{ {
var db = DbHelper.GetNewDb(); var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>(); ListClass(db);
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Book = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
var data1=db.Queryable<Student>().ToList(); ListValueType(db);
if (data1.First().Book.Count != 1) Cases.ThrowUnitError(); }
if (data1.First().Book.First().Price != 21) Cases.ThrowUnitError();
data1.First().Book.First().Price = 100; private static void ListValueType(SqlSugarClient db)
db.Updateable(data1).ExecuteCommand(); {
var data2 = db.Queryable<Student>().ToList();
if (data2.First().Book.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.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();
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();
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.CodeFirst.InitTables<IdsModel>(); db.CodeFirst.InitTables<IdsModel>();
db.DbMaintenance.TruncateTable<IdsModel>(); db.DbMaintenance.TruncateTable<IdsModel>();
var ids = new List<string> { ObjectId.GenerateNewId() + "" }; var ids = new List<string> { ObjectId.GenerateNewId() + "" };
var sid = ObjectId.GenerateNewId() + ""; var sid = ObjectId.GenerateNewId() + "";
db.Insertable(new IdsModel() {name="a", Ids =ids,Students=new List<Student>() { db.Insertable(new IdsModel()
{
name = "a",
Ids = ids,
Students = new List<Student>() {
new Student(){ Id =sid} new Student(){ Id =sid}
} }).ExecuteCommand(); }
}).ExecuteCommand();
db.Insertable(new IdsModel() db.Insertable(new IdsModel()
{ {
name = "b", name = "b",
@@ -87,6 +71,39 @@ namespace MongoDbTest
if (data.Students.First().Age != 100111) Cases.ThrowUnitError(); if (data.Students.First().Age != 100111) Cases.ThrowUnitError();
} }
private static void ListClass(SqlSugarClient db)
{
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();
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;
db.Updateable(data1).ExecuteCommand();
var data2 = db.Queryable<Student>().ToList();
if (data2.First().Book.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.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();
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();
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();
}
[SqlSugar.SugarTable("UnitStudentdfsds3zzz1")] [SqlSugar.SugarTable("UnitStudentdfsds3zzz1")]
public class Student : MongoDbBase public class Student : MongoDbBase
{ {