Update mongodb

This commit is contained in:
sunkaixuan
2025-07-04 09:27:33 +08:00
parent b21e615c5f
commit d13b21a9df
6 changed files with 79 additions and 8 deletions

View File

@@ -54,7 +54,7 @@ namespace MongoDb.Ado.data
// 返回IDataReader
return table.CreateDataReader();
}
private static object ConvertBsonValue(BsonValue val)
public static object ConvertBsonValue(BsonValue val)
{
if (val == null || val.IsBsonNull)
return DBNull.Value;

View File

@@ -35,6 +35,18 @@ namespace MongoDbTest
db.Insertable(new IdsModel() {name="a", Ids =ids,Students=new List<Student>() {
new Student(){ Id =sid}
} }).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();
}
[SqlSugar.SugarTable("UnitStudentdfsds3zzz1")]

View File

@@ -107,6 +107,14 @@ namespace SqlSugar.MongoDb
{
name = "ContainsArray";
}
else if (name == "Contains" && methodCallExpression.Arguments.Count == 1
&& methodCallExpression?.Object!=null
&&UtilMethods.IsCollectionOrArrayButNotByteArray(methodCallExpression.Object.Type)
&& ExpressionTool.GetParameters(methodCallExpression?.Object).Count() >0
&& ExpressionTool.GetParameters(methodCallExpression.Arguments.FirstOrDefault()).Count == 0)
{
name = "JsonArrayAny";
}
return name;
}
}

View File

@@ -637,6 +637,31 @@ namespace SqlSugar.MongoDb
return this.ContainsArray(model);
}
public override string JsonArrayAny(MethodCallExpressionModel model)
{
// 伪代码步骤:
// 1. 获取数组字段表达式 arrayField
// 2. 获取要判断的元素表达式 pars
// 3. 解析字段名和元素值
// 4. 构造 MongoDB $in 查询表达式:{ arrayField: { $in: [element] } }
// 5. 返回 JSON 字符串
var arrayFieldExpr = model.DataObject as Expression;
var elementExpr = model.Args[0].MemberValue as Expression;
// 获取字段名
BsonValue fieldName = new ExpressionVisitor(context).Visit(arrayFieldExpr);
// 获取元素值
var elementValue = ExpressionTool.DynamicInvoke(elementExpr);
if (elementValue is string s&&UtilMethods.IsValidObjectId(s))
{
elementValue = ObjectId.Parse(s);
}
// 构造 $in 查询表达式
var inDoc = new BsonDocument(fieldName.ToString(), new BsonDocument("$in", new BsonArray { BsonValue.Create(elementValue) }));
return inDoc.ToJson(UtilMethods.GetJsonWriterSettings());
}
#region Helper
private static BsonValue GetMemberName(BsonValue memberName)
{

View File

@@ -1,4 +1,5 @@
using MongoDB.Bson;
using MongoDb.Ado.data;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using NetTaste;
@@ -127,11 +128,19 @@ namespace SqlSugar.MongoDb
// 5. 反序列化每一项
foreach (var item in bsonArray)
{
if (item is BsonDocument)
{
var doc = item.AsBsonDocument;
var obj = BsonSerializer.Deserialize(doc, elementType);
resultList.Add(obj);
}
else
{
var obj = MongoDbDataReaderHelper.ConvertBsonValue(item);
resultList.Add(obj);
}
}
return resultList;
}
else

View File

@@ -4,6 +4,7 @@ using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -21,6 +22,22 @@ namespace SqlSugar.MongoDb
{
public class UtilMethods
{
public static bool IsCollectionOrArrayButNotByteArray(Type type)
{
if (type == null)
return false;
if (type == typeof(byte[]))
return false;
if (type.IsArray)
return true;
if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
return true;
return false;
}
public static bool IsValidObjectId(string id)
{
return id != null && id.Length == 24 && ObjectId.TryParse(id, out _);