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
ebb440a550
commit
b82b2de9ef
@ -26,6 +26,16 @@ namespace MongoDbTest
|
||||
|
||||
//根据bool过滤
|
||||
FilterStudentsByBool(db);
|
||||
|
||||
//更多
|
||||
InsertAndValidateStudent(db);
|
||||
}
|
||||
|
||||
private static void InsertAndValidateStudent(SqlSugarClient db)
|
||||
{
|
||||
db.Insertable(new Student() { Name = "", Bool = true, CreateDateTime = DateTime.Now }).ExecuteCommand();
|
||||
var list = db.Queryable<Student>().Where(it => string.IsNullOrEmpty(it.Name)).ToList();
|
||||
if (!string.IsNullOrEmpty(list.First().Name)) Cases.ThrowUnitError();
|
||||
}
|
||||
|
||||
private static void FilterStudentsByBool(SqlSugar.SqlSugarClient db)
|
||||
|
@ -570,16 +570,21 @@ namespace SqlSugar.MongoDb
|
||||
|
||||
public override string IsNullOrEmpty(MethodCallExpressionModel model)
|
||||
{
|
||||
// 判断字符串是否为 null 或空字符串
|
||||
// 获取字段名表达式
|
||||
var item = model.Args[0].MemberValue ?? model.DataObject;
|
||||
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
// $or: [ { $eq: [ "$field", null ] }, { $eq: [ "$field", "" ] } ]
|
||||
var orDoc = new BsonDocument("$or", new BsonArray
|
||||
{
|
||||
new BsonDocument("$eq", new BsonArray { $"${memberName}", BsonNull.Value }),
|
||||
new BsonDocument("$eq", new BsonArray { $"${memberName}", "" })
|
||||
});
|
||||
return orDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
var fieldExpr = new ExpressionVisitor(context).Visit(item as Expression);
|
||||
|
||||
string fieldName = fieldExpr.ToString();
|
||||
|
||||
// 生成:{ "$match": { "$or": [ { "Name": null }, { "Name": "" } ] } }
|
||||
var or=new BsonDocument(
|
||||
"$or", new BsonArray
|
||||
{
|
||||
new BsonDocument(fieldName, BsonNull.Value),
|
||||
new BsonDocument(fieldName, "")
|
||||
});
|
||||
|
||||
return or.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
|
||||
public override string Trim(MethodCallExpressionModel model)
|
||||
|
Loading…
Reference in New Issue
Block a user