mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 18:48:09 +08:00
Update mongodb
This commit is contained in:
@@ -55,6 +55,14 @@ namespace MongoDbTest
|
||||
if (list3.First().Name != "ss"|| list3.Count!=1) Cases.ThrowUnitError();
|
||||
var list4 = db.Queryable<Student>().Where(it => it.Age == 22222).ToList();
|
||||
if (list4.First().Name != "yy" || list4.Count != 1) Cases.ThrowUnitError();
|
||||
db.Updateable(new List<Student>()
|
||||
{
|
||||
new Student() { Name = "yy", Age = 22222, SchoolId = "3", CreateDateTime = DateTime.Now },
|
||||
new Student() { Name = "ss", Age = 33333, SchoolId = "4", CreateDateTime = DateTime.Now }
|
||||
}
|
||||
).WhereColumns(it =>new { it.Name,it.Age }).ExecuteCommand();
|
||||
var list5 = db.Queryable<Student>().Where(it => it.SchoolId == "3").ToList();
|
||||
if(list5.Count!=1|| list5.First().Name!="yy") Cases.ThrowUnitError(); ;
|
||||
}
|
||||
[SqlSugar.SugarTable("UnitStudentdghhuesd3z1")]
|
||||
public class Student : MongoDbBase
|
||||
|
@@ -16,7 +16,7 @@ namespace SqlSugar.MongoDb
|
||||
}
|
||||
public static bool IsFieldNameJson(BsonDocument doc)
|
||||
{
|
||||
if (doc.Contains("fieldName"))
|
||||
if (doc.Contains(UtilConstants.FieldName))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ namespace SqlSugar.MongoDb
|
||||
var result = new ExpressionVisitor(context).Visit(expr);
|
||||
if (result is BsonString bs)
|
||||
{
|
||||
result = new BsonDocument("fieldName", bs);
|
||||
result = new BsonDocument(UtilConstants.FieldName, bs);
|
||||
}
|
||||
return (BsonDocument)result;
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
@@ -26,7 +27,46 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
return Select(expr);
|
||||
}
|
||||
throw new NotSupportedException(this._context.resolveType + "");
|
||||
else if (this._context.resolveType == ResolveExpressType.ArraySingle)
|
||||
{
|
||||
return WhereColumn(expr);
|
||||
}
|
||||
HandleExpressionError(expr);
|
||||
return null;
|
||||
}
|
||||
|
||||
private BsonValue WhereColumn(Expression expr)
|
||||
{
|
||||
if (expr is NewExpression newExpr)
|
||||
{
|
||||
var bsonArray = new BsonArray();
|
||||
|
||||
// 遍历构造函数参数对应的成员
|
||||
foreach (var arg in newExpr.Arguments)
|
||||
{
|
||||
if (arg is MemberExpression memberExpr)
|
||||
{
|
||||
bsonArray.Add(memberExpr.Member.Name);
|
||||
}
|
||||
else if (arg is UnaryExpression unary && unary.Operand is MemberExpression innerMember)
|
||||
{
|
||||
// 处理装箱后的成员访问(如 object 包裹)
|
||||
bsonArray.Add(innerMember.Member.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleExpressionError(expr);
|
||||
}
|
||||
}
|
||||
return new BsonDocument(UtilConstants.FieldName,bsonArray);
|
||||
}
|
||||
HandleExpressionError(expr);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void HandleExpressionError(Expression expr)
|
||||
{
|
||||
throw new Exception(expr.ToString() + " error");
|
||||
}
|
||||
|
||||
private BsonValue Select(Expression expr)
|
||||
|
@@ -67,7 +67,7 @@ namespace SqlSugar.MongoDb
|
||||
if (MongoDbExpTools.IsFieldNameJson(trimmed))
|
||||
{
|
||||
var outerDoc = BsonDocument.Parse(trimmed);
|
||||
trimmed = outerDoc["fieldName"].AsString;
|
||||
trimmed = outerDoc[UtilConstants.FieldName].AsString;
|
||||
operations.Add(trimmed);
|
||||
}
|
||||
else
|
||||
@@ -113,9 +113,9 @@ namespace SqlSugar.MongoDb
|
||||
directionPart = "ASC";
|
||||
}
|
||||
var bson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonPart);
|
||||
if (bson.Contains("fieldName"))
|
||||
if (bson.Contains(UtilConstants.FieldName))
|
||||
{
|
||||
var field = bson["fieldName"].AsString;
|
||||
var field = bson[UtilConstants.FieldName].AsString;
|
||||
var direction = directionPart == "DESC" ? -1 : 1;
|
||||
sortDoc[field] = direction;
|
||||
}
|
||||
@@ -137,7 +137,7 @@ namespace SqlSugar.MongoDb
|
||||
});
|
||||
if (MongoDbExpTools.IsFieldNameJson(dos))
|
||||
{
|
||||
dos["fieldName"] = "$"+ dos["fieldName"];
|
||||
dos[UtilConstants.FieldName] = "$"+ dos[UtilConstants.FieldName];
|
||||
dos.Add(new BsonElement("_id", "0"));
|
||||
}
|
||||
else if (dos.ElementCount > 0 && dos.GetElement(0).Name.StartsWith("$"))
|
||||
@@ -145,7 +145,7 @@ namespace SqlSugar.MongoDb
|
||||
// 如果第一个key带有$,说明是个函数,外面套一层fieldName
|
||||
var funcDoc = new BsonDocument(dos); // 复制一份
|
||||
dos.Clear();
|
||||
dos.Add("fieldName", funcDoc);
|
||||
dos.Add(UtilConstants.FieldName, funcDoc);
|
||||
dos.Add(new BsonElement("_id", "0"));
|
||||
}
|
||||
var json = dos.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
@@ -174,9 +174,9 @@ namespace SqlSugar.MongoDb
|
||||
var bsonArray = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonArray>(jsonPart);
|
||||
foreach (BsonDocument bson in bsonArray)
|
||||
{
|
||||
if (bson.Contains("fieldName"))
|
||||
if (bson.Contains(UtilConstants.FieldName))
|
||||
{
|
||||
var field = bson["fieldName"].AsString;
|
||||
var field = bson[UtilConstants.FieldName].AsString;
|
||||
operations[operations.Count - 1] = operations[operations.Count - 1].Replace($"\"${field}\"", $"\"$_id.{field}\"");
|
||||
fieldNames.Add(field);
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
internal static class UtilConstants
|
||||
{
|
||||
public const string FieldName= "fieldName";
|
||||
public const string Dot = ".";
|
||||
public const char DotChar = '.';
|
||||
internal const string Space = " ";
|
||||
|
Reference in New Issue
Block a user