Update mongodb

This commit is contained in:
sunkaixuan 2025-05-06 20:04:39 +08:00
parent 3c46dde2a4
commit 82ee75c9ca
2 changed files with 22 additions and 10 deletions

View File

@ -1,4 +1,5 @@
using MongoDbTest.DBHelper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
@ -39,6 +40,10 @@ namespace MongoDbTest
var list4 = db.Queryable<OrderInfo>().OrderByDescending(it=>it.Price).ToList();
var list5 = db.Queryable<OrderInfo>().OrderByDescending(it => it.Price).ToList();
var list6 = db.Queryable<OrderInfo>().OrderByDescending(it => new { it.Id,Name=it.Name }).ToList();
//测试生成SQL性能
TestSqlBuilder(db);
}

View File

@ -88,20 +88,27 @@ namespace SqlSugar.MongoDb
{
order = order.Substring("ORDER BY ".Length).Trim();
int lastSpace = order.LastIndexOf(' ');
string jsonPart = order.Substring(0, lastSpace).Trim();
string directionPart = order.Substring(lastSpace + 1).Trim().ToUpper();
var sortDoc = new BsonDocument();
foreach (var str in order.Split(","))
{
int lastSpace = str.LastIndexOf(' ');
string jsonPart = str.Substring(0, lastSpace).Trim();
string directionPart = str.Substring(lastSpace + 1).Trim().ToUpper();
var bson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonPart);
if (bson.Contains("fieldName"))
{
var field = bson["fieldName"].AsString;
var direction = directionPart == "ASC" ? 1 : -1;
operations.Add($"{{ \"$sort\": {{ \"{field}\": {direction} }} }}");
var direction = directionPart == "DESC" ? -1 : 1;
sortDoc[field] = direction;
}
}
if (sortDoc.ElementCount > 0)
{
operations.Add($"{{ \"$sort\": {sortDoc.ToJson()} }}");
}
}
#endregion
sb.Append($"aggregate {this.GetTableNameString} ");
sb.Append("[");
sb.Append(string.Join(", ", operations));