fix(mongodb): guard aggregate execution paths

This commit is contained in:
徐文彬 committed 2026-05-28 21:29:08 +08:00
1 parent c56bcb8e2d
commit 5ea146e08e
19 files changed
+166 -26

No files matched your search

+7 -1
View File
@@ -83,6 +83,7 @@ pub async fn aggregate_documents(
database: &str,
collection: &str,
pipeline_json: &str,
max_rows: Option<usize>,
) -> Result<MongoDocumentResult, String> {
let json: serde_json::Value =
serde_json::from_str(pipeline_json).map_err(|e| format!("Invalid pipeline JSON: {e}"))?;
@@ -93,12 +94,17 @@ pub async fn aggregate_documents(
.collect::<Result<Vec<Document>, String>>()?;
let col = client.database(database).collection::<Document>(collection);
let mut cursor = col.aggregate(pipeline).await.map_err(|e| e.to_string())?;
let max_rows = max_rows.unwrap_or(100);
let fetch_limit = max_rows.saturating_add(1);
let mut documents = Vec::new();
while cursor.advance().await.map_err(|e| e.to_string())? {
while documents.len() < fetch_limit && cursor.advance().await.map_err(|e| e.to_string())? {
let doc = cursor.deserialize_current().map_err(|e| e.to_string())?;
documents.push(bson_to_json(&Bson::Document(doc)));
}
let total = documents.len() as u64;
if documents.len() > max_rows {
documents.truncate(max_rows);
}
Ok(MongoDocumentResult { documents, total })
}
+2 -1
View File
@@ -77,11 +77,12 @@ pub async fn mongo_aggregate_documents_core(
database: &str,
collection: &str,
pipeline_json: &str,
max_rows: Option<usize>,
) -> Result<MongoDocumentResult, String> {
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::MongoDb(client) => {
mongo_driver::aggregate_documents(client, database, collection, pipeline_json).await
mongo_driver::aggregate_documents(client, database, collection, pipeline_json, max_rows).await
}
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support aggregate".to_string()),
_ => Err("Not a MongoDB connection".to_string()),
+1 -8
View File
@@ -399,14 +399,7 @@ pub async fn list_tables_core(
}
fn collection_names_to_tables(names: Vec<String>, table_type: &str) -> Vec<db::TableInfo> {
names
.into_iter()
.map(|name| db::TableInfo {
name,
table_type: table_type.to_string(),
comment: None,
})
.collect()
names.into_iter().map(|name| db::TableInfo { name, table_type: table_type.to_string(), comment: None }).collect()
}
fn filter_table_infos(tables: Vec<db::TableInfo>, filter: Option<&str>, limit: Option<usize>) -> Vec<db::TableInfo> {