mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 04:35:29 +08:00
Update mongodb
This commit is contained in:
parent
44e251d8fb
commit
06d2db0368
@ -18,7 +18,7 @@ namespace MongoDbTest
|
||||
db.CodeFirst.InitTables<Student>();
|
||||
db.DbMaintenance.TruncateTable<Student>();
|
||||
var dt = DateTime.Now;
|
||||
var studentId = db.Insertable(new Student() { CreateDateTime=dt, Name="a", SchoolId="aa" })
|
||||
var studentId = db.Insertable(new Student() { CreateDateTime=dt,Age=11, Name="a", SchoolId="aa" })
|
||||
.ExecuteCommand();
|
||||
var list=db.Queryable<Student>().Select(it => new
|
||||
{
|
||||
@ -42,6 +42,10 @@ namespace MongoDbTest
|
||||
date = it.CreateDateTime.ToString("yyyy-MM-dd")
|
||||
}).ToList();
|
||||
if(list2.First().date!=dt.ToString("yyyy-MM-dd")) Cases.ThrowUnitError();
|
||||
var list3 = db.Queryable<Student>().Select(it => new
|
||||
{
|
||||
date = it.Age.ToString()
|
||||
}).ToList();
|
||||
}
|
||||
[SqlSugar.SugarTable("UnitStudent1231sds3z1")]
|
||||
public class Student : MongoDbBase
|
||||
@ -50,6 +54,8 @@ namespace MongoDbTest
|
||||
|
||||
public string SchoolId { get; set; }
|
||||
|
||||
public int Age { get; set; }
|
||||
|
||||
public DateTime CreateDateTime { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -158,6 +158,63 @@ namespace SqlSugar.MongoDb
|
||||
public class MongoDbMethod : DefaultDbMethod, IDbMethods
|
||||
{
|
||||
public MongoNestedTranslatorContext context { get; set; }
|
||||
public override string ToInt32(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
var toIntDoc = new BsonDocument("$toInt", $"${memberName}");
|
||||
return toIntDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string ToInt64(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
var toLongDoc = new BsonDocument("$toLong", $"${memberName}");
|
||||
return toLongDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string ToGuid(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
// MongoDB 没有直接的 Guid 类型,通常以字符串存储
|
||||
var toStringDoc = new BsonDocument("$toString", $"${memberName}");
|
||||
return toStringDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string ToDouble(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
var toDoubleDoc = new BsonDocument("$toDouble", $"${memberName}");
|
||||
return toDoubleDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string ToBool(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
var toBoolDoc = new BsonDocument("$toBool", $"${memberName}");
|
||||
return toBoolDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string ToDate(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
var toDateDoc = new BsonDocument("$toDate", $"${memberName}");
|
||||
return toDateDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string ToTime(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.DataObject as Expression;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
// MongoDB 没有单独的 Time 类型,通常用字符串或日期处理
|
||||
var toStringDoc = new BsonDocument("$toString", $"${memberName}");
|
||||
return toStringDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
public override string AggregateCount(MethodCallExpressionModel model)
|
||||
{
|
||||
var index = context.queryBuilder.LambdaExpressions.Index;
|
||||
|
Loading…
Reference in New Issue
Block a user