Update mongodb

This commit is contained in:
sunkaixuan 2025-06-21 13:19:54 +08:00
parent ebb440a550
commit b82b2de9ef
2 changed files with 24 additions and 9 deletions

View File

@ -26,6 +26,16 @@ namespace MongoDbTest
//根据bool过滤 //根据bool过滤
FilterStudentsByBool(db); 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) private static void FilterStudentsByBool(SqlSugar.SqlSugarClient db)

View File

@ -570,16 +570,21 @@ namespace SqlSugar.MongoDb
public override string IsNullOrEmpty(MethodCallExpressionModel model) public override string IsNullOrEmpty(MethodCallExpressionModel model)
{ {
// 判断字符串是否为 null 或空字符串 // 获取字段名表达式
var item = model.Args[0].MemberValue ?? model.DataObject; var item = model.Args[0].MemberValue ?? model.DataObject;
BsonValue memberName = new ExpressionVisitor(context).Visit(item as Expression); var fieldExpr = new ExpressionVisitor(context).Visit(item as Expression);
// $or: [ { $eq: [ "$field", null ] }, { $eq: [ "$field", "" ] } ]
var orDoc = new BsonDocument("$or", new BsonArray string fieldName = fieldExpr.ToString();
{
new BsonDocument("$eq", new BsonArray { $"${memberName}", BsonNull.Value }), // 生成:{ "$match": { "$or": [ { "Name": null }, { "Name": "" } ] } }
new BsonDocument("$eq", new BsonArray { $"${memberName}", "" }) var or=new BsonDocument(
}); "$or", new BsonArray
return orDoc.ToJson(UtilMethods.GetJsonWriterSettings()); {
new BsonDocument(fieldName, BsonNull.Value),
new BsonDocument(fieldName, "")
});
return or.ToJson(UtilMethods.GetJsonWriterSettings());
} }
public override string Trim(MethodCallExpressionModel model) public override string Trim(MethodCallExpressionModel model)