Update mongodb

This commit is contained in:
sunkaixuan
2025-05-02 15:34:15 +08:00
parent 4f2d7550fc
commit 9cc2c3ee0d

View File

@@ -9,19 +9,16 @@ namespace MongoDb.Ado.data
{ {
public class ExecuteScalarHandler public class ExecuteScalarHandler
{ {
public object Handle(IMongoCollection<BsonDocument> collection, string json) public object Handle(string operation, IMongoCollection<BsonDocument> collection, string json)
{ {
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json); using (var dbReader = new DbDataReaderFactory().Handle(operation, collection, json))
// 执行查询并限制返回一条记录
var document = collection.Find(filter).FirstOrDefault();
// 如果查询到结果且文档非空,则获取第一个字段的值
if (document != null && document.Elements.Any())
{ {
var firstElement = document.Elements.First(); // 获取第一个字段(列) if (dbReader.Read())
return firstElement.Value; // 返回该字段的值 {
} return dbReader.GetValue(0);
return null; // }
} return null; //
}
}
} }
} }