mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 23:13:42 +08:00
Update mongodb
This commit is contained in:
parent
e24acf4fce
commit
ac88ba26f6
@ -4,6 +4,8 @@ using System.Data;
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using MongoDB.Bson.Serialization;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MongoDb.Ado.data
|
namespace MongoDb.Ado.data
|
||||||
{
|
{
|
||||||
@ -58,12 +60,71 @@ namespace MongoDb.Ado.data
|
|||||||
|
|
||||||
if (operation == "insert")
|
if (operation == "insert")
|
||||||
{
|
{
|
||||||
|
// 处理插入操作
|
||||||
var doc = BsonDocument.Parse(json);
|
var doc = BsonDocument.Parse(json);
|
||||||
collection.InsertOne(doc);
|
collection.InsertOne(doc);
|
||||||
return 1;
|
return 1; // 返回插入成功的文档数
|
||||||
|
}
|
||||||
|
if (operation == "insertmany")
|
||||||
|
{
|
||||||
|
// 处理插入多条记录操作
|
||||||
|
var documents = BsonSerializer.Deserialize<List<BsonDocument>>(json); // 假设 json 是包含多个文档的数组
|
||||||
|
collection.InsertMany(documents);
|
||||||
|
return documents.Count; // 返回插入成功的文档数
|
||||||
|
}
|
||||||
|
if (operation == "update")
|
||||||
|
{
|
||||||
|
// 处理更新操作
|
||||||
|
var updateCommand = BsonDocument.Parse(json);
|
||||||
|
var filter = updateCommand["filter"].AsBsonDocument;
|
||||||
|
var update = updateCommand["update"].AsBsonDocument;
|
||||||
|
var options = updateCommand.Contains("options") ? updateCommand["options"].AsBsonDocument : null;
|
||||||
|
|
||||||
|
var updateResult = collection.UpdateOne(filter, update); // 单个更新
|
||||||
|
return (int)updateResult.ModifiedCount; // 返回修改的文档数
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new NotSupportedException("只支持 insert 操作。");
|
if (operation == "updatemany")
|
||||||
|
{
|
||||||
|
// 处理更新多个文档操作
|
||||||
|
var updateCommand = BsonDocument.Parse(json);
|
||||||
|
var filter = updateCommand["filter"].AsBsonDocument;
|
||||||
|
var update = updateCommand["update"].AsBsonDocument;
|
||||||
|
var options = updateCommand.Contains("options") ? updateCommand["options"].AsBsonDocument : null;
|
||||||
|
|
||||||
|
var updateResult = collection.UpdateMany(filter, update); // 多个更新
|
||||||
|
return (int)updateResult.ModifiedCount; // 返回修改的文档数
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operation == "delete")
|
||||||
|
{
|
||||||
|
// 处理删除单个文档操作
|
||||||
|
var deleteCommand = BsonDocument.Parse(json);
|
||||||
|
var filter = deleteCommand["filter"].AsBsonDocument;
|
||||||
|
|
||||||
|
var deleteResult = collection.DeleteOne(filter); // 单个删除
|
||||||
|
return (int)deleteResult.DeletedCount; // 返回删除的文档数
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operation == "deletemany")
|
||||||
|
{
|
||||||
|
// 处理删除多个文档操作
|
||||||
|
var deleteCommand = BsonDocument.Parse(json);
|
||||||
|
var filter = deleteCommand["filter"].AsBsonDocument;
|
||||||
|
|
||||||
|
var deleteResult = collection.DeleteMany(filter); // 多个删除
|
||||||
|
return (int)deleteResult.DeletedCount; // 返回删除的文档数
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operation == "find")
|
||||||
|
{
|
||||||
|
// 处理查询操作,查询并返回 0
|
||||||
|
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
|
||||||
|
var document = collection.Find(filter).FirstOrDefault(); // 查询操作
|
||||||
|
return 0; // 返回 0,表示查询操作已执行,且没有对数据库做更改
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotSupportedException("不支持此操作类型。");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object ExecuteScalar()
|
public override object ExecuteScalar()
|
||||||
|
@ -44,6 +44,14 @@ namespace MongoDbTest
|
|||||||
var value=mongoDbCommand.ExecuteScalar();
|
var value=mongoDbCommand.ExecuteScalar();
|
||||||
connection.Close();
|
connection.Close();
|
||||||
}
|
}
|
||||||
|
//ExecuteNonQuery
|
||||||
|
{
|
||||||
|
var connection = new MongoDbConnection(DbHelper.SqlSugarConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
MongoDbCommand mongoDbCommand = new MongoDbCommand(" insertMany b [{ name: \"John\", age: 31 }, { name: \"Alice\", age: 25 }, { name: \"Bob\", age: 30 } ] ", connection);
|
||||||
|
var value = mongoDbCommand.ExecuteNonQuery();
|
||||||
|
connection.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MongoDbConnectionTest()
|
private static void MongoDbConnectionTest()
|
||||||
|
Loading…
Reference in New Issue
Block a user