mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 18:22:23 +08:00
Update mongodb
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Data.Common;
|
||||
using System.Data;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System.Linq;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
@@ -73,11 +74,24 @@ namespace MongoDb.Ado.data
|
||||
if (operation == "find")
|
||||
{
|
||||
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
|
||||
var document = collection.Find(filter).FirstOrDefault();
|
||||
return document;
|
||||
|
||||
// 设置投影排除 "_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; // 返回该字段的值
|
||||
}
|
||||
|
||||
throw new NotSupportedException("只支持 find 操作。");
|
||||
return null; // 如果没有结果或没有字段,返回 null
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
||||
|
@@ -20,6 +20,8 @@ namespace MongoDbTest
|
||||
}
|
||||
|
||||
private static void MongoDbCommandTest()
|
||||
{
|
||||
//ExecuteReader
|
||||
{
|
||||
var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString);
|
||||
connection.Open();
|
||||
@@ -34,6 +36,15 @@ namespace MongoDbTest
|
||||
}
|
||||
connection.Close();
|
||||
}
|
||||
//ExecuteScalar
|
||||
{
|
||||
var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString);
|
||||
connection.Open();
|
||||
MongoDbCommand mongoDbCommand = new MongoDbCommand(" find b { age: { $gt: 31 } }", connection);
|
||||
var value=mongoDbCommand.ExecuteScalar();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void MongoDbConnectionTest()
|
||||
{
|
||||
|
Reference in New Issue
Block a user