|
| 1 | +# MongoDB Tools — Agent Instructions |
| 2 | + |
| 3 | +## Working Style |
| 4 | + |
| 5 | +We prefer that AI agents start by planning their work, documenting their plan as a Markdown file, |
| 6 | +and then iterating on that plan with the human operator. Don't just jump straight to coding in most |
| 7 | +cases. For small, well-scoped tasks (e.g., a one or two file change), it's fine to skip the planning |
| 8 | +step. |
| 9 | + |
| 10 | +Plans should be broken up into multiple steps. For larger projects, the plan should consider how to |
| 11 | +break the work up into multiple pull requests as well. We prefer PRs to be 200-400 lines. The |
| 12 | +exception would be a large rename or refactoring (like a type change), where the PR is very large in |
| 13 | +terms of lines but conceptually small. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +**The most important thing for these tools is that they must not corrupt or lose user data.** This |
| 18 | +is particularly important for `mongodump` and `mongorestore`. When we make changes to these tools, |
| 19 | +we should ensure that data is preserved via a dump and restore round trip. |
| 20 | + |
| 21 | +## Project Overview |
| 22 | + |
| 23 | +This repo contains the official MongoDB command-line tools: `bsondump`, `mongodump`, `mongorestore`, |
| 24 | +`mongoimport`, `mongoexport`, `mongostat`, `mongotop`, and `mongofiles`. |
| 25 | + |
| 26 | +Each tool lives in its own top-level directory (e.g., `/mongodump/`). Shared utilities are in |
| 27 | +`/common/`. |
| 28 | + |
| 29 | +## Build |
| 30 | + |
| 31 | +```bash |
| 32 | +go run build.go build # build all tools |
| 33 | +go run build.go build pkgs=<pkg> # build a specific tool |
| 34 | +``` |
| 35 | + |
| 36 | +Binaries are written to `./bin/`. |
| 37 | + |
| 38 | +## Tests |
| 39 | + |
| 40 | +Tests use environment variables to control which test types run. |
| 41 | + |
| 42 | +```bash |
| 43 | +go run build.go test:unit # unit tests (no mongod required) |
| 44 | +go run build.go test:integration # requires mongod on localhost:33333 |
| 45 | +go run build.go test:sharded-integration # requires mongos (sharded cluster) |
| 46 | +``` |
| 47 | + |
| 48 | +Optional flags for `test:integration`: `ssl=true`, `auth=true`, `topology=replset`, `race=true`. |
| 49 | + |
| 50 | +All tests use `testtype.SkipUnlessTestType(t, testtype.XxxTestType)` at the top to gate on the |
| 51 | +relevant env var. When writing new tests, match the existing pattern. |
| 52 | + |
| 53 | +Integration tests default to connecting to `localhost:33333`. Override with |
| 54 | +`TOOLS_TESTING_MONGOD=<uri>`. |
| 55 | + |
| 56 | +Before running integration or sharded integration tests, you must have a MongoDB server or cluster |
| 57 | +running and accessible. For basic integration tests, start a standalone `mongod` on port 33333. For |
| 58 | +sharded integration tests, start a `mongos` fronting a sharded cluster. These servers are not |
| 59 | +started automatically by the test runner. |
| 60 | + |
| 61 | +Use `mlaunch` (from the `mtools` Python package) to manage clusters during development: |
| 62 | + |
| 63 | +```bash |
| 64 | +mlaunch init --single --port 33333 # standalone mongod for integration tests |
| 65 | +mlaunch init --sharded 1 --port 33333 # sharded cluster for sharded integration tests |
| 66 | +mlaunch stop # stop all launched processes |
| 67 | +mlaunch start # restart previously initialized cluster |
| 68 | +``` |
| 69 | + |
| 70 | +## Static Analysis |
| 71 | + |
| 72 | +Our static analysis tools are managed locally by `mise`. Always modify the `mise.toml` file to add |
| 73 | +or update tools. But we _also_ manage these in CI via code in `buildscript`, so this must be updated |
| 74 | +in sync with the `mise.toml` file. |
| 75 | + |
| 76 | +```bash |
| 77 | +go run build.go sa:lint # runs precious (golangci-lint, gosec, goimports, golines) |
| 78 | +go run build.go sa:modtidy # go mod tidy |
| 79 | +``` |
| 80 | + |
| 81 | +## Code Conventions |
| 82 | + |
| 83 | +- Use `any` instead of `interface{}`. |
| 84 | +- Avoid comments that describe _what_ code does; only comment on _why_ when the reason isn't |
| 85 | + obvious. |
| 86 | +- Break large functions into smaller named functions rather than adding explanatory comments. |
| 87 | +- Write test assertion messages that describe what is being tested. |
| 88 | +- New tests should use `testify` (`github.com/stretchr/testify/require` and `assert`). The codebase |
| 89 | + is actively migrating away from GoConvey — do not add new GoConvey tests. |
| 90 | + |
| 91 | +## Architecture |
| 92 | + |
| 93 | +``` |
| 94 | +/common/ shared utilities (auth, db, bsonutil, options, testutil, testtype, …) |
| 95 | +/<tool>/ tool implementation |
| 96 | +/<tool>/main/ CLI entry point (main package) |
| 97 | +/<tool>/options.go CLI flag definitions |
| 98 | +/<tool>/*_test.go tests co-located with source |
| 99 | +``` |
| 100 | + |
| 101 | +Shared connection and session management lives in `common/db/`. CLI options are parsed via |
| 102 | +`common/options/` using `github.com/urfave/cli/v2`. |
| 103 | + |
| 104 | +## CI |
| 105 | + |
| 106 | +CI runs on Evergreen (config: `common.yml`). Do not edit `common.yml` by hand for test topology |
| 107 | +changes — consult the existing pattern for `integration-X.Y` and `integration-X.Y-sharded` task |
| 108 | +variants. |
| 109 | + |
| 110 | +## Minimal Change Principle |
| 111 | + |
| 112 | +Make minimal, incremental changes. Do not refactor surrounding code when fixing a bug or adding a |
| 113 | +feature. Do not add error handling for scenarios that cannot occur; instead, add a `panic` to handle |
| 114 | +that case. |
| 115 | + |
| 116 | +## Code in `common` is Used in Other Projects |
| 117 | + |
| 118 | +The code in `common` is used by other projects at MongoDB. If you want to modify public APIs in that |
| 119 | +package, double check with the human directing you before doing this. |
| 120 | + |
| 121 | +## Trust These Instructions |
| 122 | + |
| 123 | +These instructions are comprehensive and tested. Only perform additional exploration if: |
| 124 | + |
| 125 | +- Information here is incomplete for your specific task |
| 126 | +- Instructions are found to be incorrect or outdated |
| 127 | +- You need details about internal implementation not covered here |
0 commit comments