feat(mongodb): support bulkWrite

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-17 12:54:21 +08:00
1 parent 3f3c21aae9
commit f78dc1ba16
25 files changed
+1089 -18

No files matched your search

+16 -3
View File
@@ -1042,6 +1042,7 @@ pub enum AgentCapability {
MongoRunCommand,
MongoInsertDocuments,
MongoReplaceDocument,
MongoBulkWrite,
MongoFindCursor,
MultiSession,
StructuredErrorV1,
@@ -1125,7 +1126,7 @@ fn parse_agent_rpc_error_header(header: &str) -> (Option<i64>, String) {
}
impl AgentCapability {
pub const ALL: [Self; 26] = [
pub const ALL: [Self; 27] = [
Self::Connect,
Self::TestConnection,
Self::Metadata,
@@ -1149,6 +1150,7 @@ impl AgentCapability {
Self::MongoRunCommand,
Self::MongoInsertDocuments,
Self::MongoReplaceDocument,
Self::MongoBulkWrite,
Self::MongoFindCursor,
Self::MultiSession,
Self::StructuredErrorV1,
@@ -1179,6 +1181,7 @@ impl AgentCapability {
Self::MongoRunCommand => "mongo_run_command",
Self::MongoInsertDocuments => "mongo_insert_documents",
Self::MongoReplaceDocument => "mongo_replace_document",
Self::MongoBulkWrite => "mongo_bulk_write",
Self::MongoFindCursor => "mongo_find_cursor",
Self::MultiSession => "multi_session",
Self::StructuredErrorV1 => "structured_error_v1",
@@ -1383,6 +1386,7 @@ pub enum MongoAgentMethod {
UpdateDocument,
UpdateDocuments,
ReplaceDocument,
BulkWrite,
DeleteDocument,
DeleteDocuments,
RunCommand,
@@ -1392,7 +1396,7 @@ pub enum MongoAgentMethod {
}
impl MongoAgentMethod {
pub const ALL: [Self; 26] = [
pub const ALL: [Self; 27] = [
Self::ListDatabases,
Self::ListCollections,
Self::FindDocuments,
@@ -1413,6 +1417,7 @@ impl MongoAgentMethod {
Self::UpdateDocument,
Self::UpdateDocuments,
Self::ReplaceDocument,
Self::BulkWrite,
Self::DeleteDocument,
Self::DeleteDocuments,
Self::RunCommand,
@@ -1443,6 +1448,7 @@ impl MongoAgentMethod {
Self::UpdateDocument => "update_document",
Self::UpdateDocuments => "update_documents",
Self::ReplaceDocument => "replace_document",
Self::BulkWrite => "bulk_write",
Self::DeleteDocument => "delete_document",
Self::DeleteDocuments => "delete_documents",
Self::RunCommand => "run_command",
@@ -2849,6 +2855,10 @@ impl AgentDriverClient {
self.call_mongo_method(MongoAgentMethod::ReplaceDocument, params).await
}
pub async fn mongo_bulk_write<T: DeserializeOwned + Send + 'static>(&mut self, params: Value) -> Result<T, String> {
self.call_mongo_method(MongoAgentMethod::BulkWrite, params).await
}
pub async fn mongo_delete_document<T: DeserializeOwned + Send + 'static>(
&mut self,
params: Value,
@@ -3005,6 +3015,7 @@ pub fn agent_supports_capability(handshake: Option<&AgentHandshake>, capability:
| AgentCapability::MongoRunCommand
| AgentCapability::MongoInsertDocuments
| AgentCapability::MongoReplaceDocument
| AgentCapability::MongoBulkWrite
| AgentCapability::MongoFindCursor
) {
return handshake.map(|value| value.supports(capability)).unwrap_or(false);
@@ -5040,10 +5051,11 @@ for line in sys.stdin:
assert_eq!(AgentCapability::MongoRunCommand.as_str(), "mongo_run_command");
assert_eq!(AgentCapability::MongoInsertDocuments.as_str(), "mongo_insert_documents");
assert_eq!(AgentCapability::MongoReplaceDocument.as_str(), "mongo_replace_document");
assert_eq!(AgentCapability::MongoBulkWrite.as_str(), "mongo_bulk_write");
assert_eq!(AgentCapability::MongoFindCursor.as_str(), "mongo_find_cursor");
assert_eq!(AgentCapability::MultiSession.as_str(), "multi_session");
assert_eq!(AgentCapability::StructuredErrorV1.as_str(), "structured_error_v1");
assert_eq!(AgentCapability::ALL.len(), 26);
assert_eq!(AgentCapability::ALL.len(), 27);
}
#[test]
@@ -5106,6 +5118,7 @@ for line in sys.stdin:
assert_eq!(MongoAgentMethod::UpdateDocument.as_str(), "update_document");
assert_eq!(MongoAgentMethod::UpdateDocuments.as_str(), "update_documents");
assert_eq!(MongoAgentMethod::ReplaceDocument.as_str(), "replace_document");
assert_eq!(MongoAgentMethod::BulkWrite.as_str(), "bulk_write");
assert_eq!(MongoAgentMethod::DeleteDocument.as_str(), "delete_document");
assert_eq!(MongoAgentMethod::DeleteDocuments.as_str(), "delete_documents");
assert_eq!(MongoAgentMethod::StartFindCursor.as_str(), "start_find_cursor");
+147
View File
@@ -2120,6 +2120,153 @@ pub async fn update_documents(
Ok(result.modified_count)
}
/// Counts reported by `bulkWrite`, mirroring the shell's `BulkWriteResult`.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MongoBulkWriteResult {
pub inserted_count: u64,
pub matched_count: u64,
pub modified_count: u64,
pub deleted_count: u64,
pub upserted_count: u64,
}
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct MongoBulkWriteOptions {
ordered: Option<bool>,
}
/// bulkWrite(operations[, { ordered }]).
///
/// The Rust driver's `bulk_write` is client-level and needs MongoDB 8.0, so the
/// operations run here one at a time in order, which is what the shell's
/// collection-level bulkWrite guarantees anyway (it is not transactional). With
/// `ordered: true` (the default) the first failure stops the batch; with
/// `ordered: false` every operation is attempted and the failures are reported together.
pub async fn bulk_write(
client: &Client,
database: &str,
collection: &str,
operations_json: &str,
options_json: Option<&str>,
) -> Result<MongoBulkWriteResult, String> {
use crate::mongo_shell::BulkWriteOperation;
let operations = crate::mongo_shell::parse_bulk_write_operations(operations_json)?;
let ordered = match options_json.filter(|value| !value.trim().is_empty()) {
Some(raw) => {
let options: MongoBulkWriteOptions =
serde_json::from_str(raw).map_err(|e| format!("Invalid bulkWrite options: {e}"))?;
options.ordered.unwrap_or(true)
}
None => true,
};
let col = client.database(database).collection::<Document>(collection);
let mut result = MongoBulkWriteResult::default();
let mut failures = Vec::new();
for (index, operation) in operations.iter().enumerate() {
let outcome: Result<(), String> = async {
match operation {
BulkWriteOperation::InsertOne { document } => {
let document = json_object_to_document(document).map_err(|e| format!("Invalid document: {e}"))?;
col.insert_one(document).await.map_err(|e| e.to_string())?;
result.inserted_count += 1;
}
BulkWriteOperation::UpdateOne { filter, update, upsert, array_filters }
| BulkWriteOperation::UpdateMany { filter, update, upsert, array_filters } => {
let many = matches!(operation, BulkWriteOperation::UpdateMany { .. });
let filter = json_filter_to_document(filter).map_err(|e| format!("Invalid filter: {e}"))?;
let update = json_update_to_modifications(update).map_err(|e| format!("Invalid update: {e}"))?;
let array_filters = array_filters
.as_ref()
.and_then(serde_json::Value::as_array)
.map(|filters| filters.iter().map(json_filter_to_document).collect::<Result<Vec<_>, _>>())
.transpose()
.map_err(|e| format!("Invalid arrayFilters: {e}"))?;
let update_result = if many {
let mut action = col.update_many(filter, update);
if let Some(upsert) = upsert {
action = action.upsert(*upsert);
}
if let Some(filters) = array_filters {
action = action.array_filters(filters);
}
action.await.map_err(|e| e.to_string())?
} else {
let mut action = col.update_one(filter, update);
if let Some(upsert) = upsert {
action = action.upsert(*upsert);
}
if let Some(filters) = array_filters {
action = action.array_filters(filters);
}
action.await.map_err(|e| e.to_string())?
};
result.matched_count += update_result.matched_count;
result.modified_count += update_result.modified_count;
result.upserted_count += u64::from(update_result.upserted_id.is_some());
}
BulkWriteOperation::ReplaceOne { filter, replacement, upsert } => {
let filter = json_filter_to_document(filter).map_err(|e| format!("Invalid filter: {e}"))?;
let replacement =
json_object_to_document(replacement).map_err(|e| format!("Invalid replacement: {e}"))?;
let mut action = col.replace_one(filter, replacement);
if let Some(upsert) = upsert {
action = action.upsert(*upsert);
}
let replace_result = action.await.map_err(|e| e.to_string())?;
result.matched_count += replace_result.matched_count;
result.modified_count += replace_result.modified_count;
result.upserted_count += u64::from(replace_result.upserted_id.is_some());
}
BulkWriteOperation::DeleteOne { filter } | BulkWriteOperation::DeleteMany { filter } => {
let many = matches!(operation, BulkWriteOperation::DeleteMany { .. });
let filter = json_filter_to_document(filter).map_err(|e| format!("Invalid filter: {e}"))?;
let delete_result = if many {
col.delete_many(filter).await.map_err(|e| e.to_string())?
} else {
col.delete_one(filter).await.map_err(|e| e.to_string())?
};
result.deleted_count += delete_result.deleted_count;
}
}
Ok(())
}
.await;
if let Err(error) = outcome {
let message = format!("operation {} ({}): {error}", index + 1, operation.kind());
if ordered {
return Err(format!(
"bulkWrite stopped at {message}. Completed before it: {}",
describe_bulk_counts(&result)
));
}
failures.push(message);
}
}
if failures.is_empty() {
Ok(result)
} else {
Err(format!(
"bulkWrite finished with {} failed operation(s): {}. Completed: {}",
failures.len(),
failures.join("; "),
describe_bulk_counts(&result)
))
}
}
fn describe_bulk_counts(result: &MongoBulkWriteResult) -> String {
format!(
"inserted {}, matched {}, modified {}, deleted {}, upserted {}",
result.inserted_count, result.matched_count, result.modified_count, result.deleted_count, result.upserted_count
)
}
/// replaceOne(filter, replacement[, { upsert }]): the whole document is swapped, so
/// unlike update there are no array filters to apply.
pub async fn replace_document(