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

116 lines
6.1 KiB
C#
Raw Normal View History

2025-07-04 08:27:46 +08:00
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using SqlSugar;
2025-07-02 11:13:10 +08:00
using SqlSugar.MongoDb;
2025-06-23 20:43:14 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
public class QueryJsonArray
{
internal static void Init()
{
2025-06-25 14:33:11 +08:00
var db = DbHelper.GetNewDb();
2025-06-23 20:43:14 +08:00
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();
2025-06-23 20:52:01 +08:00
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();
2025-07-02 11:13:10 +08:00
var exp=Expressionable.Create<Student>().ToExpression();
var data3 = db.Queryable<Student>().Where(exp).ToList();
2025-07-07 18:08:36 +08:00
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();
2025-07-07 20:20:15 +08:00
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();
2025-08-15 11:51:41 +08:00
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();
2025-07-04 08:27:46 +08:00
db.CodeFirst.InitTables<IdsModel>();
2025-07-04 08:36:36 +08:00
db.DbMaintenance.TruncateTable<IdsModel>();
2025-07-04 08:27:46 +08:00
var ids = new List<string> { ObjectId.GenerateNewId() + "" };
2025-07-04 08:36:36 +08:00
var sid =ObjectId.GenerateNewId() + "";
db.Insertable(new IdsModel() {name="a", Ids =ids,Students=new List<Student>() {
new Student(){ Id =sid}
2025-07-04 09:27:33 +08:00
} }).ExecuteCommand();
db.Insertable(new IdsModel()
{
name = "b",
Ids = new List<string> { ObjectId.GenerateNewId()+"" },
Students = new List<Student>() {
new Student(){ Id =ObjectId.GenerateNewId()+""}
}
}).ExecuteCommand();
var x = ids.Last();
var list2=db.Queryable<IdsModel>().Where(it => it.Ids.Contains(x)).ToList();
if (list2.Count != 1) Cases.ThrowUnitError();
if (!list2.First().Ids.Contains(x)) Cases.ThrowUnitError();
2025-07-08 17:53:37 +08:00
var list3 = db.Queryable<IdsModel>().Where(it => !it.Ids.Contains(x)).ToList();
2025-07-08 17:56:00 +08:00
if(list3.Any(s=>s.Ids.Contains(x))) Cases.ThrowUnitError();
2025-07-24 16:04:19 +08:00
db.Insertable(new IdsModel()
{
name = "b",
Ids = new List<string> { ObjectId.GenerateNewId() + "", ObjectId.GenerateNewId() + "" },
Students = new List<Student>() {
new Student(){ Id =ObjectId.GenerateNewId()+""}
}
}).ExecuteCommand();
var list4 = db.Queryable<IdsModel>().Select(it => it.Ids.Count()).ToList();
if (list4.Last()!=2) Cases.ThrowUnitError();
2025-08-01 11:58:29 +08:00
var list6= db.Queryable<IdsModel>().Select(it => new IdsModel { Students= it.Students }).ToList();
var list5 = db.Queryable<IdsModel>().Select(it => new { it.Students}).ToList();
if (list5.Last().Students.First().Id!= list6.Last().Students.First().Id) Cases.ThrowUnitError();
2025-06-23 20:43:14 +08:00
}
[SqlSugar.SugarTable("UnitStudentdfsds3zzz1")]
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 List<Book> Book { get; set; }
}
2025-07-04 08:27:46 +08:00
public class IdsModel
{
public string name { get; set; }
[SugarColumn(IsJson =true)]
public List<string> Ids { get; set; }
2025-07-04 08:36:36 +08:00
[SugarColumn(IsJson = true)]
public List<Student> Students { get; set; }
2025-07-04 08:27:46 +08:00
}
2025-06-23 20:43:14 +08:00
public class Book
{
public decimal Price { get; set; }
public DateTime CreateTime { get; set; }
2025-07-07 20:20:15 +08:00
[BsonRepresentation(BsonType.ObjectId)]
[SqlSugar.SugarColumn(ColumnDataType = nameof(ObjectId))]
public string SId { get; set; }
2025-06-23 20:43:14 +08:00
}
}
}