mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-21 02:58:05 +08:00
Update mongodb
This commit is contained in:
@@ -15,15 +15,28 @@ namespace MongoDbTest
|
|||||||
internal static void Init()
|
internal static void Init()
|
||||||
{
|
{
|
||||||
var db = DBHelper.DbHelper.GetNewDb();
|
var db = DBHelper.DbHelper.GetNewDb();
|
||||||
db.CodeFirst.InitTables<Student>(); ;
|
db.CodeFirst.InitTables<Student>();
|
||||||
|
db.DbMaintenance.TruncateTable<Student>();
|
||||||
var dt = DateTime.Now;
|
var dt = DateTime.Now;
|
||||||
var studentId = db.Insertable(new Student() { CreateDateTime=dt, Name="a", SchoolId="aa" })
|
var studentId = db.Insertable(new Student() { CreateDateTime=dt, Name="a", SchoolId="aa" })
|
||||||
.ExecuteCommand();
|
.ExecuteCommand();
|
||||||
var list=db.Queryable<Student>().Select(it => new
|
var list=db.Queryable<Student>().Select(it => new
|
||||||
{
|
{
|
||||||
date=it.CreateDateTime.Date
|
date=it.CreateDateTime.Date,
|
||||||
|
year=it.CreateDateTime.Year,
|
||||||
|
day=it.CreateDateTime.Day,
|
||||||
|
hour=it.CreateDateTime.Hour,
|
||||||
|
Minute = it.CreateDateTime.Minute,
|
||||||
|
month=it.CreateDateTime.Month,
|
||||||
|
Second=it.CreateDateTime.Second
|
||||||
}).ToList();
|
}).ToList();
|
||||||
if (list.First().date != dt.Date) Cases.ThrowUnitError();
|
if (list.First().date != dt.Date) Cases.ThrowUnitError();
|
||||||
|
if (list.First().year != dt.Date.Year) Cases.ThrowUnitError();
|
||||||
|
if (list.First().month != dt.Date.Month) Cases.ThrowUnitError();
|
||||||
|
if (list.First().day != dt.Date.Day) Cases.ThrowUnitError();
|
||||||
|
if (list.First().hour != dt.Hour) Cases.ThrowUnitError();
|
||||||
|
if (list.First().Minute != dt.Minute) Cases.ThrowUnitError();
|
||||||
|
if (list.First().Second != dt.Second) Cases.ThrowUnitError();
|
||||||
}
|
}
|
||||||
[SqlSugar.SugarTable("UnitStudent1231sds3z1")]
|
[SqlSugar.SugarTable("UnitStudent1231sds3z1")]
|
||||||
public class Student : MongoDbBase
|
public class Student : MongoDbBase
|
||||||
|
@@ -12,7 +12,16 @@ namespace SqlSugar.MongoDb
|
|||||||
{
|
{
|
||||||
MongoNestedTranslatorContext _context;
|
MongoNestedTranslatorContext _context;
|
||||||
ExpressionVisitorContext _visitorContext;
|
ExpressionVisitorContext _visitorContext;
|
||||||
|
Dictionary<string, DateType> dateTypeNames = new Dictionary<string, DateType>
|
||||||
|
{
|
||||||
|
{ "Year", DateType.Year },
|
||||||
|
{ "Month", DateType.Month },
|
||||||
|
{ "Day", DateType.Day },
|
||||||
|
{ "Hour", DateType.Hour },
|
||||||
|
{ "Minute", DateType.Minute },
|
||||||
|
{ "Second", DateType.Second },
|
||||||
|
{ "Millisecond", DateType.Millisecond }
|
||||||
|
};
|
||||||
public FieldPathExtractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
public FieldPathExtractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
@@ -38,7 +47,12 @@ namespace SqlSugar.MongoDb
|
|||||||
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = memberExp.Expression });
|
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = memberExp.Expression });
|
||||||
return BsonDocument.Parse(method.ToDateShort(model));
|
return BsonDocument.Parse(method.ToDateShort(model));
|
||||||
}
|
}
|
||||||
|
else if (dateTypeNames.TryGetValue(memberExp.Member.Name, out var dateType))
|
||||||
|
{
|
||||||
|
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = memberExp.Expression });
|
||||||
|
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = dateType });
|
||||||
|
return BsonDocument.Parse(method.DateValue(model));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ExtractFieldPath(expr);
|
return ExtractFieldPath(expr);
|
||||||
}
|
}
|
||||||
|
@@ -305,5 +305,48 @@ namespace SqlSugar.MongoDb
|
|||||||
});
|
});
|
||||||
return dateTruncDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
return dateTruncDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
}
|
}
|
||||||
|
public override string DateValue(MethodCallExpressionModel model)
|
||||||
|
{
|
||||||
|
var item = model.Args.First().MemberValue;
|
||||||
|
BsonValue itemValue = new ExpressionVisitor(context).Visit(item as Expression);
|
||||||
|
var dateType = (DateType)model.Args.Last().MemberValue;
|
||||||
|
switch (dateType)
|
||||||
|
{
|
||||||
|
case DateType.Year:
|
||||||
|
// MongoDB $year 操作符
|
||||||
|
return new BsonDocument("$year", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Month:
|
||||||
|
return new BsonDocument("$month", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Day:
|
||||||
|
return new BsonDocument("$dayOfMonth", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Hour:
|
||||||
|
return new BsonDocument("$hour", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Minute:
|
||||||
|
return new BsonDocument("$minute", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Second:
|
||||||
|
return new BsonDocument("$second", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Millisecond:
|
||||||
|
return new BsonDocument("$millisecond", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Weekday:
|
||||||
|
return new BsonDocument("$week", $"${itemValue}").ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
case DateType.Quarter:
|
||||||
|
// MongoDB 没有直接的quarter操作符,需自定义表达式
|
||||||
|
var expr = new BsonDocument("$add", new BsonArray
|
||||||
|
{
|
||||||
|
new BsonDocument("$divide", new BsonArray
|
||||||
|
{
|
||||||
|
new BsonDocument("$subtract", new BsonArray
|
||||||
|
{
|
||||||
|
new BsonDocument("$month", $"${itemValue}"),
|
||||||
|
1
|
||||||
|
}),
|
||||||
|
3
|
||||||
|
}),
|
||||||
|
1
|
||||||
|
});
|
||||||
|
return expr.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user