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

157 lines
8.3 KiB
C#
Raw Normal View History

2025-07-04 08:27:46 +08:00
using MongoDB.Bson;
2025-08-24 12:13:05 +08:00
using MongoDB.Bson.Serialization;
2025-07-04 08:27:46 +08:00
using MongoDB.Bson.Serialization.Attributes;
2025-08-24 12:13:05 +08:00
using MongoDB.Driver;
2025-07-04 08:27:46 +08:00
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;
2025-08-24 12:13:05 +08:00
using System.Linq.Expressions;
2025-06-23 20:43:14 +08:00
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-08-23 20:29:13 +08:00
ListClass(db);
ListValueType(db);
}
private static void ListValueType(SqlSugarClient db)
{
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-08-23 20:29:13 +08:00
var sid = ObjectId.GenerateNewId() + "";
db.Insertable(new IdsModel()
{
name = "a",
Ids = ids,
Students = new List<Student>() {
2025-07-04 08:36:36 +08:00
new Student(){ Id =sid}
2025-08-23 20:29:13 +08:00
}
}).ExecuteCommand();
2025-07-04 09:27:33 +08:00
db.Insertable(new IdsModel()
{
name = "b",
2025-08-23 20:29:13 +08:00
Ids = new List<string> { ObjectId.GenerateNewId() + "" },
2025-07-04 09:27:33 +08:00
Students = new List<Student>() {
new Student(){ Id =ObjectId.GenerateNewId()+""}
}
}).ExecuteCommand();
var x = ids.Last();
2025-08-23 20:29:13 +08:00
var list2 = db.Queryable<IdsModel>().Where(it => it.Ids.Contains(x)).ToList();
2025-07-04 09:27:33 +08:00
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-08-23 20:29:13 +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();
2025-08-23 20:29:13 +08:00
if (list4.Last() != 2) Cases.ThrowUnitError();
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-08-20 16:55:23 +08:00
2025-08-23 20:29:13 +08:00
var p = new List<Student>() { new Student() { Age = 100111 } };
2025-08-20 16:55:23 +08:00
db.Updateable<IdsModel>()
2025-08-23 20:29:13 +08:00
.SetColumns(it => it.Students == p)
.Where(s => s.name == "b").ExecuteCommand();
var data = db.Queryable<IdsModel>().Where(s => s.name == "b").First();
2025-08-20 16:55:23 +08:00
if (data.Students.First().Age != 100111) Cases.ThrowUnitError();
2025-06-23 20:43:14 +08:00
}
2025-08-23 20:29:13 +08:00
private static void ListClass(SqlSugarClient db)
{
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
2025-08-24 15:38:20 +08:00
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Books = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
2025-08-23 20:29:13 +08:00
var data1 = db.Queryable<Student>().ToList();
2025-08-24 15:38:20 +08:00
if (data1.First().Books.Count != 1) Cases.ThrowUnitError();
if (data1.First().Books.First().Price != 21) Cases.ThrowUnitError();
data1.First().Books.First().Price = 100;
2025-08-23 20:29:13 +08:00
db.Updateable(data1).ExecuteCommand();
var data2 = db.Queryable<Student>().ToList();
2025-08-24 15:38:20 +08:00
if (data2.First().Books.First().Price != 100) Cases.ThrowUnitError();
2025-08-23 20:29:13 +08:00
var exp = Expressionable.Create<Student>().ToExpression();
var data3 = db.Queryable<Student>().Where(exp).ToList();
2025-08-24 15:38:20 +08:00
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();
2025-08-23 20:29:13 +08:00
db.DbMaintenance.TruncateTable<Student>();
var id = ObjectId.GenerateNewId() + "";
2025-08-24 15:38:20 +08:00
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();
2025-08-23 20:29:13 +08:00
if (data6.Count != 1 || data6.First().Name != "a") Cases.ThrowUnitError();
2025-08-24 15:38:20 +08:00
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();
2025-08-23 20:29:13 +08:00
if (data7.Count != 1 || data8.Count != 1) Cases.ThrowUnitError();
if (data7.FirstOrDefault().Name != "price=age" || data8.FirstOrDefault().Name != "price=age") Cases.ThrowUnitError();
2025-08-24 15:38:20 +08:00
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();
2025-08-24 13:10:40 +08:00
if (data13.First().Name != "call") Cases.ThrowUnitError();
2025-08-24 15:38:20 +08:00
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();
2025-08-24 13:10:40 +08:00
if(data14.First().Name!= "adddays1") Cases.ThrowUnitError();
2025-08-24 15:38:20 +08:00
var data15 = db.Queryable<Student>().Where(it => it.Books.Any(s =>s.BookChildList.Any())).ToList();
2025-08-24 16:10:31 +08:00
if (data15.Any(it=>it.Books.Any(s=>s.BookChildList.Any())==false)) Cases.ThrowUnitError();
2025-08-23 20:29:13 +08:00
}
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)]
2025-08-24 15:38:20 +08:00
public List<Book> Books { get; set; }
2025-06-23 20:43:14 +08:00
}
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
{
2025-08-24 13:10:40 +08:00
public string TimeStr { get; set; }
public string NumStr { get; set; }
2025-06-23 20:43:14 +08:00
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-08-24 12:13:05 +08:00
2025-08-24 15:38:20 +08:00
public List<Book> BookChildList { get; set; }
2025-06-23 20:43:14 +08:00
}
}
}