mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-17 21:49:33 +08:00
Update mongodb
This commit is contained in:
parent
25ec702d23
commit
e24acf4fce
@ -3,6 +3,7 @@ using System.Data.Common;
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace MongoDb.Ado.data
|
namespace MongoDb.Ado.data
|
||||||
{
|
{
|
||||||
@ -73,11 +74,24 @@ namespace MongoDb.Ado.data
|
|||||||
if (operation == "find")
|
if (operation == "find")
|
||||||
{
|
{
|
||||||
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
|
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; // 返回该字段的值
|
||||||
|
}
|
||||||
|
|
||||||
|
return null; // 如果没有结果或没有字段,返回 null
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new NotSupportedException("只支持 find 操作。");
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
||||||
|
@ -21,18 +21,29 @@ namespace MongoDbTest
|
|||||||
|
|
||||||
private static void MongoDbCommandTest()
|
private static void MongoDbCommandTest()
|
||||||
{
|
{
|
||||||
var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString);
|
//ExecuteReader
|
||||||
connection.Open();
|
{
|
||||||
MongoDbCommand mongoDbCommand = new MongoDbCommand(" find b { age: { $gt: 31 } }", connection);
|
var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString);
|
||||||
using (var reader = mongoDbCommand.ExecuteReader())
|
connection.Open();
|
||||||
{
|
MongoDbCommand mongoDbCommand = new MongoDbCommand(" find b { age: { $gt: 31 } }", connection);
|
||||||
while (reader.Read())
|
using (var reader = mongoDbCommand.ExecuteReader())
|
||||||
{
|
{
|
||||||
var name=reader.GetString("name");
|
while (reader.Read())
|
||||||
var age = reader.GetInt32("age");
|
{
|
||||||
|
var name = reader.GetString("name");
|
||||||
|
var age = reader.GetInt32("age");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
connection.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MongoDbConnectionTest()
|
private static void MongoDbConnectionTest()
|
||||||
|
Loading…
Reference in New Issue
Block a user