diff --git a/agents/common/src/main/java/com/dbx/agent/AgentProtocol.java b/agents/common/src/main/java/com/dbx/agent/AgentProtocol.java index 5c2a20dd14..c0e340aa9f 100644 --- a/agents/common/src/main/java/com/dbx/agent/AgentProtocol.java +++ b/agents/common/src/main/java/com/dbx/agent/AgentProtocol.java @@ -50,6 +50,7 @@ public final class AgentProtocol { public static final String MONGO_METHOD_FIND_DOCUMENTS = "find_documents"; public static final String MONGO_METHOD_FIND_ONE = "find_one"; public static final String MONGO_METHOD_EXPLAIN_FIND = "explain_find"; + public static final String MONGO_METHOD_AGGREGATE_DOCUMENTS = "aggregate_documents"; /** * MongoDB read path that returns documents as relaxed Extended JSON for transfer. */ @@ -237,6 +238,7 @@ public final class AgentProtocol { MONGO_METHOD_FIND_DOCUMENTS, MONGO_METHOD_FIND_ONE, MONGO_METHOD_EXPLAIN_FIND, + MONGO_METHOD_AGGREGATE_DOCUMENTS, MONGO_METHOD_FIND_DOCUMENTS_EXTENDED_JSON, MONGO_METHOD_COUNT_DOCUMENTS, MONGO_METHOD_SERVER_VERSION, diff --git a/agents/common/src/main/resources/agent-protocol-v1.json b/agents/common/src/main/resources/agent-protocol-v1.json index f031752e3c..999efd3e50 100644 --- a/agents/common/src/main/resources/agent-protocol-v1.json +++ b/agents/common/src/main/resources/agent-protocol-v1.json @@ -39,6 +39,6 @@ "disconnect", "shutdown" ], - "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], + "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "aggregate_documents", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], "kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status", "etcd_compact", "etcd_defrag", "etcd_watch_start", "etcd_watch_poll", "etcd_watch_stop", "etcd_lease_list", "etcd_lease_get", "etcd_lease_grant", "etcd_lease_keepalive_once", "etcd_lease_revoke", "etcd_auth_user_list", "etcd_auth_user_get", "etcd_auth_user_add", "etcd_auth_user_delete", "etcd_auth_user_change_password", "etcd_auth_user_grant_role", "etcd_auth_user_revoke_role", "etcd_auth_role_list", "etcd_auth_role_get", "etcd_auth_role_add", "etcd_auth_role_delete", "etcd_auth_role_grant_permission", "etcd_auth_role_revoke_permission"] } diff --git a/agents/common/src/main/resources/agent-protocol-v2.json b/agents/common/src/main/resources/agent-protocol-v2.json index 4ce801bcd7..39f73d3da5 100644 --- a/agents/common/src/main/resources/agent-protocol-v2.json +++ b/agents/common/src/main/resources/agent-protocol-v2.json @@ -43,7 +43,7 @@ "disconnect", "shutdown" ], - "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], + "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "aggregate_documents", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], "kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status", "etcd_compact", "etcd_defrag", "etcd_watch_start", "etcd_watch_poll", "etcd_watch_stop", "etcd_lease_list", "etcd_lease_get", "etcd_lease_grant", "etcd_lease_keepalive_once", "etcd_lease_revoke", "etcd_auth_user_list", "etcd_auth_user_get", "etcd_auth_user_add", "etcd_auth_user_delete", "etcd_auth_user_change_password", "etcd_auth_user_grant_role", "etcd_auth_user_revoke_role", "etcd_auth_role_list", "etcd_auth_role_get", "etcd_auth_role_add", "etcd_auth_role_delete", "etcd_auth_role_grant_permission", "etcd_auth_role_revoke_permission"], "sessionField": "agentSessionId", "cursorSessionField": "sessionId" diff --git a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java b/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java index 3f9f21e3aa..beb9bd015c 100644 --- a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java +++ b/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java @@ -12,9 +12,11 @@ import com.mongodb.ConnectionString; import com.mongodb.MongoCredential; import com.mongodb.MongoClientSettings; import com.mongodb.ServerAddress; +import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; import com.mongodb.client.model.Collation; import com.mongodb.client.model.CollationAlternate; import com.mongodb.client.model.CollationCaseFirst; @@ -55,6 +57,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; @@ -462,6 +465,207 @@ public final class MongoAgent { return bsonToExtendedJson(result); } + private static Object aggregateDocuments(JsonObject params) { + MongoClient c = requireClient(); + String database = params.get("database").getAsString(); + String collection = params.get("collection").getAsString(); + List pipeline = aggregatePipeline(params); + Document options = aggregateOptions(params); + + if (aggregateExplain(options)) { + Document plan = c.getDatabase(database).runCommand(buildAggregateCommand(collection, pipeline, options)); + List> documents = new ArrayList<>(); + List extendedDocuments = new ArrayList<>(); + documents.add(bsonToJson(plan)); + extendedDocuments.add(bsonToExtendedJson(plan)); + return documentQueryResultWithExtended(documents, extendedDocuments, 1); + } + + int maxRows = aggregateMaxRows(params); + AggregateIterable iterable = c.getDatabase(database).getCollection(collection).aggregate(pipeline); + iterable = applyAggregateOptions(iterable, options); + int fetchLimit = maxRows == Integer.MAX_VALUE ? Integer.MAX_VALUE : maxRows + 1; + List> documents = new ArrayList<>(); + List extendedDocuments = new ArrayList<>(); + try (MongoCursor cursor = iterable.iterator()) { + while (documents.size() < fetchLimit && cursor.hasNext()) { + Document document = cursor.next(); + documents.add(bsonToJson(document)); + extendedDocuments.add(bsonToExtendedJson(document)); + } + } + long total = documents.size(); + if (documents.size() > maxRows) { + documents.subList(maxRows, documents.size()).clear(); + extendedDocuments.subList(maxRows, extendedDocuments.size()).clear(); + } + return documentQueryResultWithExtended(documents, extendedDocuments, total); + } + + static List aggregatePipeline(JsonObject params) { + String source = stringOrNull(params, "pipeline"); + if (source == null || source.isBlank()) { + throw new IllegalArgumentException("MongoDB aggregate pipeline is required"); + } + JsonElement parsed = JsonParser.parseString(source); + if (!parsed.isJsonArray()) { + throw new IllegalArgumentException("MongoDB aggregate pipeline must be a JSON array"); + } + List pipeline = new ArrayList<>(); + for (JsonElement stage : parsed.getAsJsonArray()) { + if (!stage.isJsonObject()) { + throw new IllegalArgumentException("Each MongoDB aggregate pipeline stage must be an object"); + } + pipeline.add(Document.parse(stage.toString())); + } + return pipeline; + } + + static Document aggregateOptions(JsonObject params) { + Document options = documentOrNull(params, "options"); + return options == null ? new Document() : options; + } + + static boolean aggregateExplain(Document options) { + Object explain = options.get("explain"); + if (explain == null) { + return false; + } + if (!(explain instanceof Boolean)) { + throw new IllegalArgumentException("MongoDB aggregate option explain must be a boolean"); + } + return (Boolean) explain; + } + + static Document buildAggregateCommand(String collection, List pipeline, Document options) { + validateAggregateOptions(options); + Document command = new Document("aggregate", collection).append("pipeline", pipeline); + for (Map.Entry entry : options.entrySet()) { + command.append(entry.getKey(), entry.getValue()); + } + if (!aggregateExplain(options) && !command.containsKey("cursor")) { + command.append("cursor", new Document()); + } + return command; + } + + private static AggregateIterable applyAggregateOptions( + AggregateIterable iterable, + Document options + ) { + validateAggregateOptions(options); + if (options.containsKey("allowDiskUse")) { + iterable = iterable.allowDiskUse(aggregateBoolean(options, "allowDiskUse")); + } + if (options.containsKey("cursor")) { + Object rawCursor = options.get("cursor"); + if (!(rawCursor instanceof Document cursor)) { + throw new IllegalArgumentException("MongoDB aggregate option cursor must be an object"); + } + for (String key : cursor.keySet()) { + if (!"batchSize".equals(key)) { + throw new IllegalArgumentException("Unsupported MongoDB aggregate cursor option: " + key); + } + } + if (cursor.containsKey("batchSize")) { + iterable = iterable.batchSize(aggregateNonNegativeInt(cursor, "batchSize")); + } + } + if (options.containsKey("maxTimeMS")) { + iterable = iterable.maxTime(aggregateNonNegativeLong(options, "maxTimeMS"), TimeUnit.MILLISECONDS); + } + if (options.containsKey("maxAwaitTimeMS")) { + iterable = iterable.maxAwaitTime( + aggregateNonNegativeLong(options, "maxAwaitTimeMS"), + TimeUnit.MILLISECONDS + ); + } + if (options.containsKey("bypassDocumentValidation")) { + iterable = iterable.bypassDocumentValidation(aggregateBoolean(options, "bypassDocumentValidation")); + } + if (options.containsKey("collation")) { + Object rawCollation = options.get("collation"); + if (!(rawCollation instanceof Document collation)) { + throw new IllegalArgumentException("MongoDB aggregate option collation must be an object"); + } + iterable = iterable.collation(collationOrNull(collation)); + } + if (options.containsKey("comment")) { + Object comment = options.get("comment"); + if (!(comment instanceof String)) { + throw new IllegalArgumentException("MongoDB aggregate option comment must be a string"); + } + iterable = iterable.comment((String) comment); + } + if (options.containsKey("hint")) { + Object hint = options.get("hint"); + if (!(hint instanceof Document)) { + throw new IllegalArgumentException("MongoDB Legacy aggregate option hint must be an object"); + } + iterable = iterable.hint((Document) hint); + } + if (options.containsKey("useCursor")) { + iterable = iterable.useCursor(aggregateBoolean(options, "useCursor")); + } + return iterable; + } + + private static void validateAggregateOptions(Document options) { + Set supported = Set.of( + "explain", + "allowDiskUse", + "cursor", + "maxTimeMS", + "maxAwaitTimeMS", + "bypassDocumentValidation", + "collation", + "comment", + "hint", + "useCursor" + ); + for (String key : options.keySet()) { + if (!supported.contains(key)) { + throw new IllegalArgumentException("Unsupported MongoDB Legacy aggregate option: " + key); + } + } + } + + private static int aggregateMaxRows(JsonObject params) { + long value = params.has("limit") ? params.get("limit").getAsLong() : 100; + if (value < 0 || value > Integer.MAX_VALUE) { + throw new IllegalArgumentException("MongoDB aggregate limit must be between 0 and " + Integer.MAX_VALUE); + } + return (int) value; + } + + private static boolean aggregateBoolean(Document options, String key) { + Object value = options.get(key); + if (!(value instanceof Boolean)) { + throw new IllegalArgumentException("MongoDB aggregate option " + key + " must be a boolean"); + } + return (Boolean) value; + } + + private static int aggregateNonNegativeInt(Document options, String key) { + long value = aggregateNonNegativeLong(options, key); + if (value > Integer.MAX_VALUE) { + throw new IllegalArgumentException("MongoDB aggregate option " + key + " is too large"); + } + return (int) value; + } + + private static long aggregateNonNegativeLong(Document options, String key) { + Object value = options.get(key); + if (!(value instanceof Number number) || number.doubleValue() != Math.rint(number.doubleValue())) { + throw new IllegalArgumentException("MongoDB aggregate option " + key + " must be a non-negative integer"); + } + long result = number.longValue(); + if (result < 0) { + throw new IllegalArgumentException("MongoDB aggregate option " + key + " must be a non-negative integer"); + } + return result; + } + static Document buildFindExplainCommand(JsonObject params) { String collection = params.get("collection").getAsString(); Document find = new Document("find", collection); @@ -681,6 +885,16 @@ public final class MongoAgent { return result; } + private static Map documentQueryResultWithExtended( + List> documents, + List extendedDocuments, + long total + ) { + Map result = documentQueryResult(documents, new CollectionTotal(total, true)); + result.put("extended_documents", extendedDocuments); + return result; + } + record CollectionTotal(long value, boolean exact) {} private static Object countDocuments(JsonObject params) { @@ -1343,6 +1557,7 @@ public final class MongoAgent { case AgentProtocol.MONGO_METHOD_FIND_DOCUMENTS -> findDocuments(params); case AgentProtocol.MONGO_METHOD_FIND_ONE -> findOne(params); case AgentProtocol.MONGO_METHOD_EXPLAIN_FIND -> explainFind(params); + case AgentProtocol.MONGO_METHOD_AGGREGATE_DOCUMENTS -> aggregateDocuments(params); case AgentProtocol.MONGO_METHOD_FIND_DOCUMENTS_EXTENDED_JSON -> findDocumentsExtendedJson(params); case AgentProtocol.MONGO_METHOD_COUNT_DOCUMENTS -> countDocuments(params); case AgentProtocol.MONGO_METHOD_SERVER_VERSION -> serverVersion(params); diff --git a/agents/drivers/mongodb/src/test/java/com/dbx/agent/mongodb/MongoAgentTest.java b/agents/drivers/mongodb/src/test/java/com/dbx/agent/mongodb/MongoAgentTest.java index 7f7bcdc20e..eb4e9326fb 100644 --- a/agents/drivers/mongodb/src/test/java/com/dbx/agent/mongodb/MongoAgentTest.java +++ b/agents/drivers/mongodb/src/test/java/com/dbx/agent/mongodb/MongoAgentTest.java @@ -13,9 +13,11 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.mongodb.MongoClientSettings; +import com.mongodb.client.AggregateIterable; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Collation; import com.mongodb.client.model.CollationStrength; @@ -34,6 +36,7 @@ import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.bson.Document; import org.bson.types.ObjectId; import org.junit.jupiter.api.BeforeAll; @@ -208,6 +211,69 @@ class MongoAgentTest { assertFalse(error.get("message").getAsString().contains("Unknown method")); } + @Test + void aggregateMethodIsRecognizedOverJsonRpc() { + String response = MongoAgent.handleRequest( + "{\"jsonrpc\":\"2.0\",\"id\":20,\"method\":\"aggregate_documents\"," + + "\"params\":{\"database\":\"app\",\"collection\":\"orders\",\"pipeline\":\"[]\"}}" + ); + + JsonObject error = JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("error"); + assertEquals("Not connected", error.get("message").getAsString()); + assertFalse(error.get("message").getAsString().contains("Unknown method")); + } + + @Test + void aggregateReadsOneBoundedCursorWithoutCounting() { + List calls = new ArrayList<>(); + MongoClient client = recordingAggregateMongoClient( + calls, + List.of( + new Document("name", "first"), + new Document("name", "second"), + new Document("name", "third") + ) + ); + String response = MongoAgent.handleRequest( + "{\"jsonrpc\":\"2.0\",\"id\":21,\"method\":\"aggregate_documents\"," + + "\"params\":{\"database\":\"app\",\"collection\":\"orders\"," + + "\"pipeline\":\"[{\\\"$match\\\":{\\\"status\\\":\\\"open\\\"}}]\"," + + "\"limit\":2," + + "\"options\":\"{\\\"allowDiskUse\\\":true,\\\"cursor\\\":{\\\"batchSize\\\":1}," + + "\\\"maxTimeMS\\\":500}\"}}", + client + ); + + JsonObject json = JsonParser.parseString(response).getAsJsonObject(); + assertFalse(json.has("error"), json.toString()); + JsonObject result = json.getAsJsonObject("result"); + assertEquals(3, result.get("total").getAsInt()); + assertEquals(2, result.getAsJsonArray("documents").size()); + assertEquals(2, result.getAsJsonArray("extended_documents").size()); + assertEquals("first", result.getAsJsonArray("documents").get(0).getAsJsonObject().get("name").getAsString()); + assertEquals(1, calls.stream().filter(call -> call.startsWith("aggregate:")).count()); + assertTrue(calls.contains("aggregate:1:open")); + assertTrue(calls.contains("allowDiskUse:true")); + assertTrue(calls.contains("batchSize:1")); + assertTrue(calls.contains("maxTime:500:MILLISECONDS")); + assertTrue(calls.contains("close")); + assertFalse(calls.stream().anyMatch(call -> call.contains("count"))); + } + + @Test + void aggregateExplainCommandPreservesPipelineAndOptions() { + List pipeline = List.of(new Document("$match", new Document("status", "open"))); + Document options = new Document("explain", true).append("allowDiskUse", true); + + Document command = MongoAgent.buildAggregateCommand("orders", pipeline, options); + + assertEquals("orders", command.getString("aggregate")); + assertEquals(pipeline, command.get("pipeline")); + assertEquals(true, command.getBoolean("explain")); + assertEquals(true, command.getBoolean("allowDiskUse")); + assertFalse(command.containsKey("cursor")); + } + @Test void findOneUsesOneBoundedReadWithoutCounting() { List calls = new ArrayList<>(); @@ -1099,6 +1165,88 @@ class MongoAgentTest { ); } + @SuppressWarnings("unchecked") + private static MongoClient recordingAggregateMongoClient(List calls, List resultDocuments) { + int[] index = {0}; + MongoCursor cursor = (MongoCursor) Proxy.newProxyInstance( + MongoCursor.class.getClassLoader(), + new Class[] {MongoCursor.class}, + (proxy, method, args) -> { + return switch (method.getName()) { + case "hasNext" -> index[0] < resultDocuments.size(); + case "next" -> resultDocuments.get(index[0]++); + case "close" -> { + calls.add("close"); + yield null; + } + default -> throw new UnsupportedOperationException(method.getName()); + }; + } + ); + + AggregateIterable[] iterableRef = new AggregateIterable[1]; + AggregateIterable iterable = (AggregateIterable) Proxy.newProxyInstance( + AggregateIterable.class.getClassLoader(), + new Class[] {AggregateIterable.class}, + (proxy, method, args) -> { + return switch (method.getName()) { + case "allowDiskUse" -> { + calls.add("allowDiskUse:" + args[0]); + yield iterableRef[0]; + } + case "batchSize" -> { + calls.add("batchSize:" + args[0]); + yield iterableRef[0]; + } + case "maxTime" -> { + calls.add("maxTime:" + args[0] + ":" + ((TimeUnit) args[1]).name()); + yield iterableRef[0]; + } + case "iterator" -> cursor; + default -> throw new UnsupportedOperationException(method.getName()); + }; + } + ); + iterableRef[0] = iterable; + + MongoCollection collection = (MongoCollection) Proxy.newProxyInstance( + MongoCollection.class.getClassLoader(), + new Class[] {MongoCollection.class}, + (proxy, method, args) -> { + if ("aggregate".equals(method.getName())) { + List pipeline = (List) args[0]; + Document match = pipeline.get(0).get("$match", Document.class); + calls.add("aggregate:" + pipeline.size() + ":" + match.getString("status")); + return iterable; + } + throw new UnsupportedOperationException(method.getName()); + } + ); + MongoDatabase database = (MongoDatabase) Proxy.newProxyInstance( + MongoDatabase.class.getClassLoader(), + new Class[] {MongoDatabase.class}, + (proxy, method, args) -> { + if ("getCollection".equals(method.getName())) { + return collection; + } + throw new UnsupportedOperationException(method.getName()); + } + ); + return (MongoClient) Proxy.newProxyInstance( + MongoClient.class.getClassLoader(), + new Class[] {MongoClient.class}, + (proxy, method, args) -> { + if ("getDatabase".equals(method.getName())) { + return database; + } + if ("close".equals(method.getName())) { + return null; + } + throw new UnsupportedOperationException(method.getName()); + } + ); + } + private static JsonObject minimalConnection() { JsonObject conn = new JsonObject(); conn.addProperty("host", "127.0.0.1"); diff --git a/crates/dbx-core/assets/agent-protocol-v1.json b/crates/dbx-core/assets/agent-protocol-v1.json index f031752e3c..999efd3e50 100644 --- a/crates/dbx-core/assets/agent-protocol-v1.json +++ b/crates/dbx-core/assets/agent-protocol-v1.json @@ -39,6 +39,6 @@ "disconnect", "shutdown" ], - "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], + "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "aggregate_documents", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], "kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status", "etcd_compact", "etcd_defrag", "etcd_watch_start", "etcd_watch_poll", "etcd_watch_stop", "etcd_lease_list", "etcd_lease_get", "etcd_lease_grant", "etcd_lease_keepalive_once", "etcd_lease_revoke", "etcd_auth_user_list", "etcd_auth_user_get", "etcd_auth_user_add", "etcd_auth_user_delete", "etcd_auth_user_change_password", "etcd_auth_user_grant_role", "etcd_auth_user_revoke_role", "etcd_auth_role_list", "etcd_auth_role_get", "etcd_auth_role_add", "etcd_auth_role_delete", "etcd_auth_role_grant_permission", "etcd_auth_role_revoke_permission"] } diff --git a/crates/dbx-core/assets/agent-protocol-v2.json b/crates/dbx-core/assets/agent-protocol-v2.json index 4ce801bcd7..39f73d3da5 100644 --- a/crates/dbx-core/assets/agent-protocol-v2.json +++ b/crates/dbx-core/assets/agent-protocol-v2.json @@ -43,7 +43,7 @@ "disconnect", "shutdown" ], - "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], + "mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_one", "explain_find", "aggregate_documents", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "drop_database", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"], "kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status", "etcd_compact", "etcd_defrag", "etcd_watch_start", "etcd_watch_poll", "etcd_watch_stop", "etcd_lease_list", "etcd_lease_get", "etcd_lease_grant", "etcd_lease_keepalive_once", "etcd_lease_revoke", "etcd_auth_user_list", "etcd_auth_user_get", "etcd_auth_user_add", "etcd_auth_user_delete", "etcd_auth_user_change_password", "etcd_auth_user_grant_role", "etcd_auth_user_revoke_role", "etcd_auth_role_list", "etcd_auth_role_get", "etcd_auth_role_add", "etcd_auth_role_delete", "etcd_auth_role_grant_permission", "etcd_auth_role_revoke_permission"], "sessionField": "agentSessionId", "cursorSessionField": "sessionId" diff --git a/crates/dbx-core/src/db/agent_driver.rs b/crates/dbx-core/src/db/agent_driver.rs index 5266778535..2e595a54d5 100644 --- a/crates/dbx-core/src/db/agent_driver.rs +++ b/crates/dbx-core/src/db/agent_driver.rs @@ -1274,6 +1274,7 @@ pub enum MongoAgentMethod { FindDocuments, FindOne, ExplainFind, + AggregateDocuments, FindDocumentsExtendedJson, CountDocuments, ServerVersion, @@ -1289,12 +1290,13 @@ pub enum MongoAgentMethod { } impl MongoAgentMethod { - pub const ALL: [Self; 17] = [ + pub const ALL: [Self; 18] = [ Self::ListDatabases, Self::ListCollections, Self::FindDocuments, Self::FindOne, Self::ExplainFind, + Self::AggregateDocuments, Self::FindDocumentsExtendedJson, Self::CountDocuments, Self::ServerVersion, @@ -1316,6 +1318,7 @@ impl MongoAgentMethod { Self::FindDocuments => "find_documents", Self::FindOne => "find_one", Self::ExplainFind => "explain_find", + Self::AggregateDocuments => "aggregate_documents", Self::FindDocumentsExtendedJson => "find_documents_extended_json", Self::CountDocuments => "count_documents", Self::ServerVersion => "server_version", @@ -2481,6 +2484,13 @@ impl AgentDriverClient { self.call_mongo_method(MongoAgentMethod::ExplainFind, params).await } + pub async fn mongo_aggregate_documents( + &mut self, + params: Value, + ) -> Result { + self.call_mongo_method(MongoAgentMethod::AggregateDocuments, params).await + } + /// Calls the Mongo agent read method that returns MongoDB relaxed Extended JSON. pub async fn mongo_find_documents_extended_json( &mut self, @@ -4333,6 +4343,7 @@ for line in sys.stdin: assert_eq!(MongoAgentMethod::FindDocuments.as_str(), "find_documents"); assert_eq!(MongoAgentMethod::FindOne.as_str(), "find_one"); assert_eq!(MongoAgentMethod::ExplainFind.as_str(), "explain_find"); + assert_eq!(MongoAgentMethod::AggregateDocuments.as_str(), "aggregate_documents"); assert_eq!(MongoAgentMethod::FindDocumentsExtendedJson.as_str(), "find_documents_extended_json"); assert_eq!(MongoAgentMethod::CountDocuments.as_str(), "count_documents"); assert_eq!(MongoAgentMethod::ServerVersion.as_str(), "server_version"); diff --git a/crates/dbx-core/src/mongo_ops.rs b/crates/dbx-core/src/mongo_ops.rs index 7a3dd2abdb..e231818517 100644 --- a/crates/dbx-core/src/mongo_ops.rs +++ b/crates/dbx-core/src/mongo_ops.rs @@ -386,7 +386,24 @@ pub async fn mongo_aggregate_documents_core( PoolKind::MongoDb(client) => { mongo_driver::aggregate_documents(client, database, collection, pipeline_json, max_rows, options_json).await } - PoolKind::Agent(_) => Err("MongoDB legacy agent does not support aggregate".to_string()), + PoolKind::Agent(client) => { + let mut client = client.lock().await; + let params = serde_json::json!({ + "database": database, + "collection": collection, + "pipeline": pipeline_json, + "limit": max_rows.unwrap_or(100), + "options": options_json, + }); + match client.mongo_aggregate_documents(params).await { + Ok(result) => Ok(result), + Err(error) if is_unknown_agent_method_error(&error, "aggregate_documents") => Err( + "MongoDB Legacy Agent does not support aggregate; upgrade or reinstall the MongoDB Legacy driver" + .to_string(), + ), + Err(error) => Err(error), + } + } _ => Err("Not a MongoDB connection".to_string()), } }