Update mongodb

This commit is contained in:
sunkaixuan 2025-06-24 20:36:06 +08:00
parent 755d9af8c6
commit 57928167c7
3 changed files with 66 additions and 3 deletions

View File

@ -66,8 +66,17 @@ namespace MongoDbTest
var dt = DateTime.Now.AddDays(-10).Date; var dt = DateTime.Now.AddDays(-10).Date;
var list70=db.Queryable<Student>().ToList().Where(it => it.CreateDateTime.Date == dt).ToList(); var list70=db.Queryable<Student>().ToList().Where(it => it.CreateDateTime.Date == dt).ToList();
var list71 = db.Queryable<Student>().Where(it => it.CreateDateTime.Date == dt).ToList(); var list71 = db.Queryable<Student>().Where(it => it.CreateDateTime.Date == dt).ToList();
if (list71.Count != list70.Count) Cases.ThrowUnitError(); if (list71.Count != list70.Count) Cases.ThrowUnitError();
var ids=list71.Select(it => it.Id).ToList();
var list72 = db.Queryable<Student>().Where(it => ids.Contains(it.Id)).ToList();
if(list72.Count!=ids.Count) Cases.ThrowUnitError();
var ids2 = db.Queryable<Student>().Select(it => it.Id).ToList();
var list73 = db.Queryable<Student>().Where(it => ids2.Contains(it.Id)).ToList();
if (list73.Count != ids2.Count) Cases.ThrowUnitError();
} }
private static void ValidateStudentData(SqlSugar.SqlSugarClient db) private static void ValidateStudentData(SqlSugar.SqlSugarClient db)

View File

@ -4,6 +4,8 @@ using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Linq; using System.Linq;
using static Dm.net.buffer.ByteArrayBuffer;
using System.Collections;
namespace SqlSugar.MongoDb namespace SqlSugar.MongoDb
{ {
public class MethodCallExpressionTractor public class MethodCallExpressionTractor
@ -25,8 +27,7 @@ namespace SqlSugar.MongoDb
{ {
var methodCallExpression = expr as MethodCallExpression; var methodCallExpression = expr as MethodCallExpression;
var name = methodCallExpression.Method.Name; var name = methodCallExpression.Method.Name;
if (name == "ToDateTime") name = TransformMethodName(methodCallExpression, name);
name = "ToDate";
BsonValue result = null; BsonValue result = null;
var context = new MongoDbMethod() { context = this._context }; var context = new MongoDbMethod() { context = this._context };
MethodCallExpressionModel model = new MethodCallExpressionModel(); MethodCallExpressionModel model = new MethodCallExpressionModel();
@ -87,5 +88,19 @@ namespace SqlSugar.MongoDb
return result; return result;
} }
} }
private static string TransformMethodName(MethodCallExpression methodCallExpression, string name)
{
if (name == "ToDateTime")
name = "ToDate";
if (name == "Contains" && methodCallExpression.Arguments.Count == 1
&& methodCallExpression?.Object?.Type != null
&& ExpressionTool.GetParameters(methodCallExpression.Object).Count == 0
&& typeof(IEnumerable).IsAssignableFrom(methodCallExpression.Object.Type))
{
name = "ContainsArray";
}
return name;
}
} }
} }

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using System.Collections;
namespace SqlSugar.MongoDb namespace SqlSugar.MongoDb
{ {
public class MongoDbExpressionContext : ExpressionContext, ILambdaExpressions public class MongoDbExpressionContext : ExpressionContext, ILambdaExpressions
@ -593,6 +594,44 @@ namespace SqlSugar.MongoDb
return trimDoc.ToJson(UtilMethods.GetJsonWriterSettings()); return trimDoc.ToJson(UtilMethods.GetJsonWriterSettings());
} }
public override string ContainsArray(MethodCallExpressionModel model)
{
// 解析数组表达式和待判断的元素表达式
var arrayExp = model.DataObject as Expression;
var itemExp = model.Args[0].MemberValue as Expression;
// 获取字段名
BsonValue fieldName = new ExpressionVisitor(context).Visit(itemExp);
// 获取数组值
var arrayObj = ExpressionTool.DynamicInvoke(arrayExp) as IEnumerable;
if (arrayObj == null)
return null;
var name=fieldName.ToString();
// 构建BsonArray
var bsonArray = new BsonArray();
foreach (var val in arrayObj)
{
if (val == null)
bsonArray.Add(BsonNull.Value);
else if (name == "_id")
{
bsonArray.Add(BsonValue.Create(ObjectId.Parse(val?.ToString())));
}
else
bsonArray.Add(BsonValue.Create(val));
}
// 构建MongoDB的 $in 查询表达式
var inDoc = new BsonDocument(name, new BsonDocument("$in", bsonArray));
return inDoc.ToJson(UtilMethods.GetJsonWriterSettings());
}
public override string ContainsArrayUseSqlParameters(MethodCallExpressionModel model)
{
return this.ContainsArray(model);
}
#region Helper #region Helper
private static BsonValue GetMemberName(BsonValue memberName) private static BsonValue GetMemberName(BsonValue memberName)
{ {