mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-23 04:23:47 +08:00
Update mongodb
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace MongoDb.Ado.data
|
||||||
|
{
|
||||||
|
public class ExecuteScalarHandler
|
||||||
|
{
|
||||||
|
public object Handle(IMongoCollection<BsonDocument> collection, string json)
|
||||||
|
{
|
||||||
|
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
|
||||||
|
|
||||||
|
// 设置投影排除 "_id" 字段,确保返回的结果不包含 _id 字段
|
||||||
|
var projection = Builders<BsonDocument>.Projection.Exclude("_id");
|
||||||
|
|
||||||
|
// 执行查询并限制返回一条记录
|
||||||
|
var document = collection.Find(filter).Project(projection).FirstOrDefault();
|
||||||
|
|
||||||
|
// 如果查询到结果且文档非空,则获取第一个字段的值
|
||||||
|
if (document != null && document.Elements.Any())
|
||||||
|
{
|
||||||
|
var firstElement = document.Elements.First(); // 获取第一个字段(列)
|
||||||
|
return firstElement.Value; // 返回该字段的值
|
||||||
|
}
|
||||||
|
return null; //
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -68,33 +68,11 @@ namespace MongoDb.Ado.data
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override object ExecuteScalar()
|
public override object ExecuteScalar()
|
||||||
{
|
{
|
||||||
var (operation, collectionName, json) = ParseCommand(_commandText);
|
var (operation, collectionName, json) = ParseCommand(_commandText);
|
||||||
var collection = GetCollection(collectionName);
|
var collection = GetCollection(collectionName);
|
||||||
|
return new ExecuteScalarHandler().Handle(collection, json);
|
||||||
if (operation == "find")
|
|
||||||
{
|
|
||||||
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
|
|
||||||
|
|
||||||
// 设置投影排除 "_id" 字段,确保返回的结果不包含 _id 字段
|
|
||||||
var projection = Builders<BsonDocument>.Projection.Exclude("_id");
|
|
||||||
|
|
||||||
// 执行查询并限制返回一条记录
|
|
||||||
var document = collection.Find(filter).Project(projection).FirstOrDefault();
|
|
||||||
|
|
||||||
// 如果查询到结果且文档非空,则获取第一个字段的值
|
|
||||||
if (document != null && document.Elements.Any())
|
|
||||||
{
|
|
||||||
var firstElement = document.Elements.First(); // 获取第一个字段(列)
|
|
||||||
return firstElement.Value; // 返回该字段的值
|
|
||||||
}
|
|
||||||
|
|
||||||
return null; // 如果没有结果或没有字段,返回 null
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
||||||
{
|
{
|
||||||
var (operation, collectionName, json) = ParseCommand(_commandText);
|
var (operation, collectionName, json) = ParseCommand(_commandText);
|
||||||
|
Reference in New Issue
Block a user