From 942d7db2fd3a96a0c3f01f04d0d6e8ba993886c5 Mon Sep 17 00:00:00 2001 From: h3n4l Date: Mon, 19 Jan 2026 16:55:00 +0800 Subject: [PATCH] feat: add cursor.count() deprecation error Return a helpful error message when cursor.count() is used, directing users to use countDocuments() or estimatedDocumentCount() instead. MongoDB drivers deprecate their respective cursor and collection count() APIs in favor of countDocuments() and estimatedDocumentCount(). Co-Authored-By: Claude Opus 4.5 --- executor_test.go | 16 ++++++++++++++++ translator.go | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/executor_test.go b/executor_test.go index 556cd0b..f27e93d 100644 --- a/executor_test.go +++ b/executor_test.go @@ -1890,3 +1890,19 @@ func TestDistinctNumericValues(t *testing.T) { require.NotNil(t, result) require.Equal(t, 3, result.RowCount) // 100, 85, 90 } + +func TestCursorCountDeprecated(t *testing.T) { + client, cleanup := setupTestContainer(t) + defer cleanup() + + ctx := context.Background() + + gc := gomongo.NewClient(client) + + // cursor.count() is deprecated and should return an error + _, err := gc.Execute(ctx, "testdb", "db.users.find().count()") + require.Error(t, err) + require.Contains(t, err.Error(), "count()") + require.Contains(t, err.Error(), "countDocuments()") + require.Contains(t, err.Error(), "estimatedDocumentCount()") +} diff --git a/translator.go b/translator.go index ae5f433..3a9a5a9 100644 --- a/translator.go +++ b/translator.go @@ -608,6 +608,12 @@ func (v *mongoShellVisitor) visitMethodCall(ctx mongodb.IMethodCallContext) { case "distinct": v.operation.opType = opDistinct v.extractDistinctArgs(gmCtx) + case "count": + // cursor.count() is deprecated + v.err = &UnsupportedOperationError{ + Operation: "count()", + Hint: "MongoDB drivers deprecate their respective cursor and collection count() APIs in favor of countDocuments() and estimatedDocumentCount()", + } default: v.err = &UnsupportedOperationError{ Operation: methodName,