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 ISODate() and new Date() in MongoDB queries
Hiep Le78019c44e237
diff --git a/apps/desktop/src/lib/mongo/mongoShellCommand.ts b/apps/desktop/src/lib/mongo/mongoShellCommand.tsindex 9add566732..49f9efb1b1 100644--- a/apps/desktop/src/lib/mongo/mongoShellCommand.ts+++ b/apps/desktop/src/lib/mongo/mongoShellCommand.ts@@ -618,7 +618,14 @@ function parseCollectionMethodTarget(source: string, method: string): { collectifunction normalizeJsonArgument(value: string): string | null {const trimmed = value.trim();if (!trimmed) return "{}";- const preprocessed = quoteUnquotedObjectKeys(convertSingleQuotedStrings(trimmed.replace(/ObjectId\s*\(\s*["']([^"']+)["']\s*\)/g, '{"$oid":"$1"}')));+ // Rewrite mongo shell constructors that are not valid JSON into the extended+ // JSON the backend understands (mongo_driver::json_value_to_bson): ObjectId(x)+ // -> {"$oid":x} and ISODate(x)/new Date(x) -> {"$date":x}. Without this a+ // filter such as { createdAt: { $gte: ISODate("...") } } fails JSON.parse,+ // the command is left unrecognized and falls through to the SQL executor,+ // which rejects it with "Use MongoDB-specific commands".+ const withExtendedJson = trimmed.replace(/ObjectId\s*\(\s*["']([^"']+)["']\s*\)/g, '{"$oid":"$1"}').replace(/(?:ISODate|new\s+Date)\s*\(\s*["']([^"']+)["']\s*\)/g, '{"$date":"$1"}');+ const preprocessed = quoteUnquotedObjectKeys(convertSingleQuotedStrings(withExtendedJson));try {JSON.parse(preprocessed);return preprocessed;diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rsindex 6cf079c851..a3290ff1fc 100644--- a/crates/dbx-core/src/db/mongo_driver.rs+++ b/crates/dbx-core/src/db/mongo_driver.rs@@ -1167,6 +1167,13 @@ fn json_filter_value_to_bson(value: &serde_json::Value, field_name: Option<&str>return Bson::ObjectId(oid);}}+ // Extended JSON dates must be decoded in filters too, otherwise+ // {"$date": ...} reaches the server as a raw document: a bare+ // { field: {"$date": ...} } fails with "unknown operator: $date"+ // and { field: {"$gte": {"$date": ...}} } silently matches nothing.+ if let Some(date) = parse_extended_json_date(obj) {+ return Bson::DateTime(date);+ }}if field_name == Some("_id") && obj.keys().all(|key| key.starts_with('$')) {@@ -1350,6 +1357,27 @@ mod tests {assert!(matches!(doc.get("owner_id"), Some(Bson::String(value)) if value == id));}+ #[test]+ fn json_filter_to_document_decodes_extended_json_dates() {+ let iso = "2025-02-25T04:57:39.965Z";+ let expected = DateTime::parse_rfc3339_str(iso).unwrap();++ // Direct equality must yield a BSON DateTime, not a raw { "$date": ... }+ // document that the server rejects with "unknown operator: $date".+ let filter = serde_json::json!({ "createdAt": { "$date": iso } });+ let doc = json_filter_to_document(&filter).unwrap();+ assert_eq!(doc.get("createdAt"), Some(&Bson::DateTime(expected)));++ // Range operands must be decoded too, otherwise $gte compares against a+ // sub-document and silently matches nothing.+ let range = serde_json::json!({ "createdAt": { "$gte": { "$date": iso } } });+ let range_doc = json_filter_to_document(&range).unwrap();+ let Some(Bson::Document(op)) = range_doc.get("createdAt") else {+ panic!("expected operator document");+ };+ assert_eq!(op.get("$gte"), Some(&Bson::DateTime(expected)));+ }+#[test]fn bson_to_json_displays_date_as_mongo_isodate() {let date = DateTime::parse_rfc3339_str("2026-06-10T13:59:31.287Z").unwrap();diff --git a/packages/app-tests/mongoShellCommand.test.ts b/packages/app-tests/mongoShellCommand.test.tsindex c01ebd266d..69d27076b5 100644--- a/packages/app-tests/mongoShellCommand.test.ts+++ b/packages/app-tests/mongoShellCommand.test.ts@@ -65,6 +65,23 @@ test("parseMongoFindCommand accepts Compass-style unquoted keys and ObjectId", (assert.deepEqual(JSON.parse(command.filter), { _id: { $oid: "6a045a92d2971e44243771a1" } });});+test("parseMongoFindCommand rewrites ISODate into extended JSON $date", () => {+ const command = parseMongoFindCommand(`db.trainingdocuments.find({+ createdAt: { $gte: ISODate("2025-02-25T04:57:39.965Z") }+ })`);+ assert.ok(command);+ assert.equal(command.collection, "trainingdocuments");+ assert.deepEqual(JSON.parse(command.filter), { createdAt: { $gte: { $date: "2025-02-25T04:57:39.965Z" } } });+});++test("parseMongoFindCommand rewrites new Date and single-quoted ISODate", () => {+ const command = parseMongoFindCommand("db.events.find({ at: { $lt: new Date('2025-01-01T00:00:00Z'), $gte: ISODate('2024-01-01T00:00:00Z') } })");+ assert.ok(command);+ assert.deepEqual(JSON.parse(command.filter), {+ at: { $lt: { $date: "2025-01-01T00:00:00Z" }, $gte: { $date: "2024-01-01T00:00:00Z" } },+ });+});+test("parseMongoFindCommand accepts single-quoted string values and unquoted sort keys", () => {const command = parseMongoFindCommand("db.products.find({category: 'Electronics'}).sort({price: -1}).limit(2)");assert.ok(command);