Update Mongodb

This commit is contained in:
sunkaixuan
2025-06-12 14:07:28 +08:00
parent b6c8b64f8c
commit a7837ddf8f
3 changed files with 25 additions and 13 deletions

View File

@@ -127,6 +127,17 @@ namespace MongoDbTest
max=SqlFunc.AggregateMax(it.Id), max=SqlFunc.AggregateMax(it.Id),
min=SqlFunc.AggregateMin(it.Id) min=SqlFunc.AggregateMin(it.Id)
}).ToList(); }).ToList();
var list14 = db.Queryable<OrderInfo>()
.GroupBy(it => new { it.Name ,it.Price })
.Select(it => new
{
key = it.Name,
Prie=it.Price,
groupCount = SqlFunc.AggregateCount(it.Id),
max = SqlFunc.AggregateMax(it.Id),
min = SqlFunc.AggregateMin(it.Id)
}).ToList();
//测试生成SQL性能 //测试生成SQL性能
TestSqlBuilder(db); TestSqlBuilder(db);
} }

View File

@@ -1,5 +1,5 @@
using MongoDbTest; using MongoDbTest;
//MongoDb还在开发中预计5月30号前可以使用 //MongoDb还在开发中预计6月30号前可以使用
OrmTest.Init(); OrmTest.Init();
AdoTest.Init(); AdoTest.Init();
ExpTest.Init(); ExpTest.Init();

View File

@@ -115,8 +115,7 @@ namespace SqlSugar.MongoDb
} }
} }
#endregion #endregion
#region Select #region Select
if (this.SelectValue is Expression expression) if (this.SelectValue is Expression expression)
{ {
@@ -129,8 +128,7 @@ namespace SqlSugar.MongoDb
operations.Add($"{{\"$project\": {json} }}"); operations.Add($"{{\"$project\": {json} }}");
} }
#endregion #endregion
#region GroupBy #region GroupBy
if (this.GroupByValue.HasValue()) if (this.GroupByValue.HasValue())
{ {
@@ -144,17 +142,20 @@ namespace SqlSugar.MongoDb
var selectItem = match.Groups[1].Value; var selectItem = match.Groups[1].Value;
selectItems.Add(selectItem); selectItems.Add(selectItem);
} }
var jsonPart = Regex.Split(this.GroupByValue, UtilConstants.ReplaceCommaKey) var jsonPart = "["+Regex.Split(this.GroupByValue, UtilConstants.ReplaceCommaKey)
.First() .First()
.TrimEnd('(') .TrimEnd('(')
.Replace("GROUP BY ", ""); .Replace("GROUP BY ", "")+"]";
var fieldNames = new List<string>(); var fieldNames = new List<string>();
var bson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonPart); var bsonArray = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonArray>(jsonPart);
if (bson.Contains("fieldName")) foreach (BsonDocument bson in bsonArray)
{ {
var field = bson["fieldName"].AsString; if (bson.Contains("fieldName"))
operations[operations.Count - 1] = operations[operations.Count - 1].Replace($"\"${field}\"", $"\"$_id.{field}\""); {
fieldNames.Add(field); var field = bson["fieldName"].AsString;
operations[operations.Count - 1] = operations[operations.Count - 1].Replace($"\"${field}\"", $"\"$_id.{field}\"");
fieldNames.Add(field);
}
} }
// 构造 _id 部分:支持多字段形式 // 构造 _id 部分:支持多字段形式
var groupId = new BsonDocument(); var groupId = new BsonDocument();