Compare commits

...

2 Commits

Author SHA1 Message Date
sunkaixuan
7832ab747d Add demo 2025-10-04 11:26:02 +08:00
sunkaixuan
a92d5ef77a Update mongodb 2025-10-04 11:25:44 +08:00
4 changed files with 54 additions and 1 deletions

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

View File

@@ -39,6 +39,7 @@ namespace MongoDbTest
Enum.Init();
Enum2.Init();
UnitDateTimeFunc.Init();
UnitDateOnly.Init();
//主键不是ObjectId类型用例
//The primary key is not an ObjectId type use case
LongPrimaryKey.Init();

View File

@@ -0,0 +1,42 @@
using MongoDB.Bson;
using SqlSugar.MongoDb;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
internal class UnitDateOnly
{
public static void Init()
{
var db = DbHelper.GetNewDb();
db.DbMaintenance.TruncateTable<DateOnlyModel>();
var dt = DateOnly.FromDateTime(Convert.ToDateTime("2022-01-01"));
var dt2 = DateOnly.FromDateTime(Convert.ToDateTime("2022-11-01"));
db.Insertable(new DateOnlyModel()
{
DateOnly=dt
}).ExecuteCommand();
db.Insertable(new DateOnlyModel()
{
DateOnly = DateOnly.FromDateTime(Convert.ToDateTime("2022-12-01"))
}).ExecuteCommand();
var list = db.Queryable<DateOnlyModel>().ToList();
var list2 = db.Queryable<DateOnlyModel>().Where(it=>it.DateOnly==dt).ToList();
var list3 = db.Queryable<DateOnlyModel>().Where(it => it.DateOnly == dt2).ToList();
if (list2.Count != 1 || list3.Count !=0 || list.Count != 2) Cases.ThrowUnitError();
}
}
public class DateOnlyModel : MongoDbBase
{
public DateOnly DateOnly { get; set; }
}
}

View File

@@ -147,6 +147,12 @@ namespace SqlSugar.MongoDb
return arrayObj;
}
}
internal static object DateOnlyToDateTime(object value)
{
if (value == null) return null;
var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToShortDateString");
return method.Invoke(value, new object[] { });
}
public static BsonValue MyCreate(object value)
{
if (value is DateTime dt)
@@ -162,6 +168,10 @@ namespace SqlSugar.MongoDb
{
value = ObjectId.Parse(s);
}
else if (value!=null&&value?.GetType()?.Name=="DateOnly")
{
value = Convert.ToDateTime(DateOnlyToDateTime(value)).ToString("yyyy-MM-dd");
}
return BsonValue.Create(value);
}
internal static MongoDB.Bson.IO.JsonWriterSettings GetJsonWriterSettings()