Update mongodb

This commit is contained in:
sunkaixuan
2025-05-11 14:19:01 +08:00
parent ece68b0246
commit 11bc16dc13
21 changed files with 84 additions and 39 deletions

View File

@@ -1,5 +1,4 @@
using Npgsql;
using SqlSugar.MongoDbCore;
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;

View File

@@ -1,5 +1,5 @@
using SqlSugar;
using SqlSugar.MongoDbCore;
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@@ -1,4 +1,4 @@
using SqlSugar.MongoDbCore;
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@@ -76,7 +76,7 @@ namespace MongoDbTest
.ExecuteCommand();
var delrow3 = db.Deleteable<OrderInfo>()
.Where(it=>it.Id==ids.Last()||it.Name=="A1")
.Where(it=>it.Id==ids.Last() )
.ExecuteCommand();
var list = db.Queryable<OrderInfo>().ToDataTable();
@@ -103,13 +103,20 @@ namespace MongoDbTest
Name=it.Name
}).ToDataTable();
var list11 = db.Queryable<OrderInfo>()
.Select(it => new
{
Id = it.Id,
Name = it.Name
}).ToDataTable();
var list11 = db.Queryable<OrderInfo>()
.Select(it => new
{
Id = it.Id,
Name = it.Name
}).ToList();
var list12 = db.Queryable<OrderInfo>()
.Select(it => new
{
Id = it.Id,
Name = it.Name+"b",
Name2 = "b"+it.Name
}).ToDataTable();
//测试生成SQL性能
TestSqlBuilder(db);
}

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class ExpressionVisitorContext
{

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class MongoNestedTranslatorContext
{

View File

@@ -6,7 +6,7 @@ using System.Data;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class ExpressionVisitor
{

View File

@@ -4,7 +4,7 @@ using System.Globalization;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class MongoDbExpTools
{

View File

@@ -5,7 +5,7 @@ using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using MongoDB.Bson;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public static class MongoNestedTranslator
{

View File

@@ -1,4 +1,5 @@
using MongoDB.Bson;
using Dm.parser;
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
using System;
@@ -7,7 +8,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class BinaryExpressionTranslator
{
@@ -63,7 +64,7 @@ namespace SqlSugar.MongoDbCore
OutParameters(expr, out field, out value, out leftIsMember, out rightIsMember, out op);
if (op == null)
{
return GetCalculationOperation(field, expr.NodeType, value);
return GetCalculationOperation(field, expr.NodeType, value,leftIsMember, rightIsMember);
}
else
{
@@ -127,7 +128,7 @@ namespace SqlSugar.MongoDbCore
if (_context?.context != null)
{
var entityInfo = _context.context.EntityMaintenance.GetEntityInfo(parameter.Type);
var columnInfo = entityInfo.Columns.FirstOrDefault(s => s.PropertyName == leftValue);
var columnInfo = entityInfo.Columns.FirstOrDefault(s => s.PropertyName == leftValue||s.DbColumnName==leftValue);
if (columnInfo != null)
{
leftValue = columnInfo.DbColumnName;
@@ -139,7 +140,17 @@ namespace SqlSugar.MongoDbCore
}
}
}
return new BsonDocument { { leftValue, rightValue } };
if (op == "$eq")
{
return new BsonDocument { { leftValue, rightValue } };
}
else
{
return new BsonDocument
{
{ leftValue, new BsonDocument { { op, rightValue } } }
};
}
}
else
{
@@ -150,7 +161,7 @@ namespace SqlSugar.MongoDbCore
}
}
private BsonDocument GetCalculationOperation(BsonValue field, ExpressionType nodeType, BsonValue value)
private BsonDocument GetCalculationOperation(BsonValue field, ExpressionType nodeType, BsonValue value, bool leftIsMember, bool rightIsMember)
{
string operation = nodeType switch
{
@@ -161,11 +172,19 @@ namespace SqlSugar.MongoDbCore
ExpressionType.Modulo => "$mod",
_ => throw new NotSupportedException($"Unsupported calculation operation: {nodeType}")
};
if (operation == "$add"&& value.BsonType==BsonType.String)
{
operation = "$concat";
return new BsonDocument
{
{ operation, new BsonArray { UtilMethods.GetBsonValue(leftIsMember, field), UtilMethods.GetBsonValue(rightIsMember, value) } }
};
;
}
return new BsonDocument
{
{ field.ToString(), new BsonDocument { { operation, value } } }
};
{
{ field.ToString(), new BsonDocument { { operation, value } } }
};
}
private static bool IsRightValue(bool leftIsMember, bool rightIsMember, string op)

View File

@@ -6,7 +6,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class FieldPathExtractor
{

View File

@@ -5,7 +5,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class MemberInitExpressionTractor
{

View File

@@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class MethodCallExpressionTractor
{

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class NewExpressionTractor
{
@@ -44,13 +44,25 @@ namespace SqlSugar.MongoDbCore
// 使用 ExpressionVisitor 访问表达式
var json = new ExpressionVisitor(_context, _visitorContext).Visit(exp.Arguments[exp.Members.IndexOf(member)]);
// 构建 MongoDB 的投影文档
projectionDocument[fieldName] = "$" + json.ToString();
SetProjectionValue(json, fieldName, projectionDocument);
}
projectionDocument["_id"] = 0;
return projectionDocument;
}
private static void SetProjectionValue(BsonValue json, string fieldName, BsonDocument projectionDocument)
{
var jsonString = json.ToJson(UtilMethods.GetJsonWriterSettings());
if (jsonString.StartsWith("{") && jsonString.EndsWith("}"))
{
projectionDocument[fieldName] = json;
}
else
{
projectionDocument[fieldName] = "$"+jsonString.TrimStart('\"').TrimEnd('\"');
}
}
private BsonValue Update(Expression expr)
{
var exp = expr as NewExpression ?? throw new InvalidOperationException("Expression must be NewExpression");

View File

@@ -5,7 +5,7 @@ using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class ValueExtractor
{

View File

@@ -1,6 +1,6 @@
using Microsoft.Data.SqlClient;
using MongoDb.Ado.data;
using SqlSugar.MongoDbCore;
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Data;

View File

@@ -1,6 +1,6 @@
using MongoDB.Bson.IO;
using MongoDB.Bson;
using SqlSugar.MongoDbCore;
using SqlSugar.MongoDb;
using System;
using System.Linq;
using System.Linq.Expressions;

View File

@@ -1,5 +1,5 @@
using MongoDB.Bson;
using SqlSugar.MongoDbCore;
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections.Generic;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class SqlSugarMongoDbDataAdapter : MongoDbDataAdapter, SqlSugar.IDataAdapter
{

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace SqlSugar.MongoDbCore
namespace SqlSugar.MongoDb
{
public class MongoDbBase
{

View File

@@ -1,4 +1,5 @@
using System;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -471,6 +472,13 @@ namespace SqlSugar.MongoDb
}
return dic;
}
internal static BsonValue GetBsonValue(bool isMember, BsonValue field)
{
if (isMember) return "$" + field;
else
return field;
}
//public static object ConvertDataByTypeName(string ctypename,string value)
//{
// var item = new ConditionalModel() {