mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update mongodb
This commit is contained in:
parent
b3da93082b
commit
c6ca767eb7
@ -122,8 +122,8 @@ namespace MongoDbTest
|
|||||||
.GroupBy(it => it.Name)
|
.GroupBy(it => it.Name)
|
||||||
.Select(it => new
|
.Select(it => new
|
||||||
{
|
{
|
||||||
Id = it.Name,
|
Name = it.Name,
|
||||||
MyCount = SqlFunc.AggregateCount(it.Id)
|
x = SqlFunc.AggregateCount(it.Id)
|
||||||
}).ToList();
|
}).ToList();
|
||||||
//测试生成SQL性能
|
//测试生成SQL性能
|
||||||
TestSqlBuilder(db);
|
TestSqlBuilder(db);
|
||||||
|
@ -166,14 +166,7 @@ namespace SqlSugar.MongoDb
|
|||||||
{
|
{
|
||||||
arg = "$" + str.AsString;
|
arg = "$" + str.AsString;
|
||||||
}
|
}
|
||||||
var countExpression = new BsonDocument(name, new BsonDocument("$sum",
|
var countExpression = new BsonDocument(name, new BsonDocument("$sum",1));
|
||||||
new BsonDocument("$cond", new BsonArray
|
|
||||||
{
|
|
||||||
new BsonDocument("$ifNull", new BsonArray { arg, false }),
|
|
||||||
1,
|
|
||||||
0
|
|
||||||
})
|
|
||||||
));
|
|
||||||
var result= countExpression.ToJson(SqlSugar.MongoDb.UtilMethods.GetJsonWriterSettings());
|
var result= countExpression.ToJson(SqlSugar.MongoDb.UtilMethods.GetJsonWriterSettings());
|
||||||
context.queryBuilder.GroupByValue += $"({UtilConstants.ReplaceCommaKey}({result}){UtilConstants.ReplaceCommaKey})";
|
context.queryBuilder.GroupByValue += $"({UtilConstants.ReplaceCommaKey}({result}){UtilConstants.ReplaceCommaKey})";
|
||||||
context.queryBuilder.LambdaExpressions.Index++;
|
context.queryBuilder.LambdaExpressions.Index++;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MongoDB.Bson;
|
using Dm.util;
|
||||||
|
using MongoDB.Bson;
|
||||||
using SqlSugar.MongoDb;
|
using SqlSugar.MongoDb;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -115,6 +116,7 @@ namespace SqlSugar.MongoDb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Select
|
#region Select
|
||||||
if (this.SelectValue is Expression expression)
|
if (this.SelectValue is Expression expression)
|
||||||
@ -129,6 +131,57 @@ namespace SqlSugar.MongoDb
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region GroupBy
|
||||||
|
if (this.GroupByValue.HasValue())
|
||||||
|
{
|
||||||
|
var regex = new Regex($@"\(\{UtilConstants.ReplaceCommaKey}\((.*?)\)\{UtilConstants.ReplaceCommaKey}\)",
|
||||||
|
RegexOptions.Compiled);
|
||||||
|
|
||||||
|
var matches = regex.Matches(this.GroupByValue);
|
||||||
|
var selectItems = new List<string>();
|
||||||
|
foreach (Match match in matches)
|
||||||
|
{
|
||||||
|
var selectItem = match.Groups[1].Value;
|
||||||
|
selectItems.Add(selectItem);
|
||||||
|
}
|
||||||
|
var jsonPart = Regex.Split(this.GroupByValue, UtilConstants.ReplaceCommaKey)
|
||||||
|
.First()
|
||||||
|
.TrimEnd('(')
|
||||||
|
.replace("GROUP BY ", "");
|
||||||
|
var fieldNames = new List<string>();
|
||||||
|
var bson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonPart);
|
||||||
|
if (bson.Contains("fieldName"))
|
||||||
|
{
|
||||||
|
var field = bson["fieldName"].AsString;
|
||||||
|
fieldNames.add(field);
|
||||||
|
}
|
||||||
|
// 构造 _id 部分:支持多字段形式
|
||||||
|
var groupId = new BsonDocument();
|
||||||
|
foreach (var field in fieldNames)
|
||||||
|
{
|
||||||
|
groupId.Add(field, $"${field}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var groupDoc = new BsonDocument("$group", new BsonDocument
|
||||||
|
{
|
||||||
|
{ "_id", groupId }
|
||||||
|
});
|
||||||
|
|
||||||
|
// 解析 selectJsonItems(每个是 BsonDocument 字符串)
|
||||||
|
var groupFields = groupDoc["$group"].AsBsonDocument;
|
||||||
|
foreach (var json in selectItems)
|
||||||
|
{
|
||||||
|
var doc = BsonDocument.Parse(json);
|
||||||
|
foreach (var element in doc)
|
||||||
|
{
|
||||||
|
groupFields.Add(element.Name, element.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
operations.Insert(operations.Count-1, groupDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
sb.Append($"aggregate {this.GetTableNameString} ");
|
sb.Append($"aggregate {this.GetTableNameString} ");
|
||||||
sb.Append("[");
|
sb.Append("[");
|
||||||
sb.Append(string.Join(", ", operations));
|
sb.Append(string.Join(", ", operations));
|
||||||
|
Loading…
Reference in New Issue
Block a user