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-04 08:36:36 +08:00
|
|
|
|
|
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-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; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|