mirror of
				https://gitee.com/dotnetchina/SqlSugar.git
				synced 2025-10-31 16:36:50 +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;  // 返回该字段的值 | ||||
|                 } | ||||
|  | ||||
|                 return null; // 如果没有结果或没有字段,返回 null | ||||
|             } | ||||
|  | ||||
|             throw new NotSupportedException("只支持 find 操作。"); | ||||
|            return null; | ||||
|         } | ||||
|  | ||||
|         protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) | ||||
|   | ||||
| @@ -21,18 +21,29 @@ namespace MongoDbTest | ||||
|  | ||||
|         private static void MongoDbCommandTest() | ||||
|         { | ||||
|             var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString); | ||||
|             connection.Open(); | ||||
|             MongoDbCommand mongoDbCommand = new MongoDbCommand(" find b { age: { $gt: 31 } }", connection); | ||||
|             using (var reader = mongoDbCommand.ExecuteReader())  | ||||
|             { | ||||
|                 while (reader.Read())  | ||||
|             //ExecuteReader | ||||
|             {  | ||||
|                 var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString); | ||||
|                 connection.Open(); | ||||
|                 MongoDbCommand mongoDbCommand = new MongoDbCommand(" find b { age: { $gt: 31 } }", connection); | ||||
|                 using (var reader = mongoDbCommand.ExecuteReader()) | ||||
|                 { | ||||
|                     var name=reader.GetString("name"); | ||||
|                     var age = reader.GetInt32("age"); | ||||
|                     while (reader.Read()) | ||||
|                     { | ||||
|                         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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 sunkaixuan
					sunkaixuan