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.MongoDb;
using SqlSugar.MongoDbCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
using MongoDB.Bson; using Dm.parser;
using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
@@ -7,7 +8,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
namespace SqlSugar.MongoDbCore namespace SqlSugar.MongoDb
{ {
public class BinaryExpressionTranslator public class BinaryExpressionTranslator
{ {
@@ -63,7 +64,7 @@ namespace SqlSugar.MongoDbCore
OutParameters(expr, out field, out value, out leftIsMember, out rightIsMember, out op); OutParameters(expr, out field, out value, out leftIsMember, out rightIsMember, out op);
if (op == null) if (op == null)
{ {
return GetCalculationOperation(field, expr.NodeType, value); return GetCalculationOperation(field, expr.NodeType, value,leftIsMember, rightIsMember);
} }
else else
{ {
@@ -127,7 +128,7 @@ namespace SqlSugar.MongoDbCore
if (_context?.context != null) if (_context?.context != null)
{ {
var entityInfo = _context.context.EntityMaintenance.GetEntityInfo(parameter.Type); 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) if (columnInfo != null)
{ {
leftValue = columnInfo.DbColumnName; 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 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 string operation = nodeType switch
{ {
@@ -161,11 +172,19 @@ namespace SqlSugar.MongoDbCore
ExpressionType.Modulo => "$mod", ExpressionType.Modulo => "$mod",
_ => throw new NotSupportedException($"Unsupported calculation operation: {nodeType}") _ => 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 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) 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.Linq.Expressions;
using System.Text; using System.Text;
namespace SqlSugar.MongoDbCore namespace SqlSugar.MongoDb
{ {
public class FieldPathExtractor public class FieldPathExtractor
{ {

View File

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

View File

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

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace SqlSugar.MongoDbCore namespace SqlSugar.MongoDb
{ {
public class NewExpressionTractor public class NewExpressionTractor
{ {
@@ -44,13 +44,25 @@ namespace SqlSugar.MongoDbCore
// 使用 ExpressionVisitor 访问表达式 // 使用 ExpressionVisitor 访问表达式
var json = new ExpressionVisitor(_context, _visitorContext).Visit(exp.Arguments[exp.Members.IndexOf(member)]); var json = new ExpressionVisitor(_context, _visitorContext).Visit(exp.Arguments[exp.Members.IndexOf(member)]);
// 构建 MongoDB 的投影文档 SetProjectionValue(json, fieldName, projectionDocument);
projectionDocument[fieldName] = "$" + json.ToString();
} }
projectionDocument["_id"] = 0; projectionDocument["_id"] = 0;
return projectionDocument; 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) private BsonValue Update(Expression expr)
{ {
var exp = expr as NewExpression ?? throw new InvalidOperationException("Expression must be NewExpression"); 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.Linq.Expressions;
using System.Text; using System.Text;
namespace SqlSugar.MongoDbCore namespace SqlSugar.MongoDb
{ {
public class ValueExtractor public class ValueExtractor
{ {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
using System; using MongoDB.Bson;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@@ -471,6 +472,13 @@ namespace SqlSugar.MongoDb
} }
return dic; 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) //public static object ConvertDataByTypeName(string ctypename,string value)
//{ //{
// var item = new ConditionalModel() { // var item = new ConditionalModel() {