25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(mongo): support string document ids in update/delete
SKZ76378f3a65d5
diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rsindex 5f6ee8f31d..b15db403e4 100644--- a/crates/dbx-core/src/db/mongo_driver.rs+++ b/crates/dbx-core/src/db/mongo_driver.rs@@ -149,12 +149,16 @@ pub async fn update_document(id: &str,doc_json: &str,) -> Result<u64, String> {- let oid = mongodb::bson::oid::ObjectId::parse_str(id).map_err(|e| format!("Invalid ObjectId: {e}"))?;let mut new_doc: Document = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;new_doc.remove("_id");let col = client.database(database).collection::<Document>(collection);- let result = col.replace_one(doc! { "_id": oid }, new_doc).await.map_err(|e| e.to_string())?;- Ok(result.modified_count)+ for filter in document_id_filters(id) {+ let result = col.replace_one(filter, new_doc.clone()).await.map_err(|e| e.to_string())?;+ if result.matched_count > 0 {+ return Ok(result.modified_count);+ }+ }+ Ok(0)}pub async fn update_documents(@@ -181,10 +185,22 @@ pub async fn update_documents(}pub async fn delete_document(client: &Client, database: &str, collection: &str, id: &str) -> Result<u64, String> {- let oid = mongodb::bson::oid::ObjectId::parse_str(id).map_err(|e| format!("Invalid ObjectId: {e}"))?;let col = client.database(database).collection::<Document>(collection);- let result = col.delete_one(doc! { "_id": oid }).await.map_err(|e| e.to_string())?;- Ok(result.deleted_count)+ for filter in document_id_filters(id) {+ let result = col.delete_one(filter).await.map_err(|e| e.to_string())?;+ if result.deleted_count > 0 {+ return Ok(result.deleted_count);+ }+ }+ Ok(0)+}++fn document_id_filters(id: &str) -> Vec<Document> {+ let string_filter = doc! { "_id": Bson::String(id.to_string()) };+ match ObjectId::parse_str(id) {+ Ok(oid) => vec![doc! { "_id": Bson::ObjectId(oid) }, string_filter],+ Err(_) => vec![string_filter],+ }}pub async fn delete_documents(@@ -266,3 +282,27 @@ fn json_value_to_bson(value: &serde_json::Value) -> Bson {}}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn document_id_filters_try_object_id_then_string_for_hex_ids() {+ let id = "507f1f77bcf86cd799439011";+ let filters = document_id_filters(id);++ assert_eq!(filters.len(), 2);+ assert!(matches!(filters[0].get("_id"), Some(Bson::ObjectId(_))));+ assert!(matches!(filters[1].get("_id"), Some(Bson::String(value)) if value == id));+ }++ #[test]+ fn document_id_filters_use_string_only_for_non_hex_ids() {+ let id = "customer-42";+ let filters = document_id_filters(id);++ assert_eq!(filters.len(), 1);+ assert!(matches!(filters[0].get("_id"), Some(Bson::String(value)) if value == id));+ }+}