### Run Basic Server Setup and Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Perform the basic workflow for running tests: start the server, set up the test environment, and run the tests. ```bash just run-server just setup-tests just run-tests ``` -------------------------------- ### Install Dependencies and Pre-commit Hook with Just Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Uses the 'just' command to install a virtual environment with pre-commit and set up the necessary hooks. This simplifies the setup process for local development. ```bash just install ``` -------------------------------- ### Run Server Setup with Optional Arguments Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Start the MongoDB server with optional arguments to configure its behavior. Use `just run-server -h` for a full list of options. ```bash just run-server --ssl ``` -------------------------------- ### Install and Run Commands for PyMongo Source: https://github.com/mongodb/mongo-python-driver/blob/master/AGENTS.md Provides essential commands for setting up the development environment, running linters, executing tests (both synchronous and asynchronous), and starting a local MongoDB server. ```bash just install # set up venv + pre-commit hook just typing-mypy # type check just run lint-manual # ruff linter hatch run test:test # run sync tests hatch run test:test-async # run async tests MONGODB_VERSION=8.0 just run-server # start a local MongoDB server ``` -------------------------------- ### Run Test Setup with Optional Arguments Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Configure the test environment with optional arguments. Use `just setup-tests -h` for a full list of available options. ```bash just setup-tests --ssl ``` -------------------------------- ### Install and Configure Pre-commit Hooks Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Installs pre-commit and sets up the git hooks for local linting. Run these commands to ensure your code adheres to the project's style guidelines before committing. ```bash brew install pre-commit pre-commit install ``` -------------------------------- ### Start MongoDB Server with Run-Mongod Script Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Starts a MongoDB server using the 'run-mongodb.sh' script from the drivers-evergreen-tools repository. Various options can be passed to configure the server startup. ```bash just run-server ``` ```bash $DRIVERS_TOOLS/.evergreen/run-mongodb.sh start -h ``` -------------------------------- ### Run Load Balancer Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute load balancer tests. This requires installing haproxy, starting the server with load balancer configuration, setting up tests, and then running them. ```bash just run-server load_balancer just setup-tests load_balancer just run-tests ``` -------------------------------- ### Install memray Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Install the memray tool, which is used for memory profiling. This is a prerequisite for detecting memory leaks and other memory-related issues. ```bash pip install memray ``` -------------------------------- ### Install PyMongo Source: https://github.com/mongodb/mongo-python-driver/blob/master/README.md Commands to install the PyMongo package using pip. ```bash python -m pip install pymongo ``` ```bash pip install . ``` -------------------------------- ### Serve Documentation Locally for Live Preview Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Starts a local server to serve the documentation, enabling live re-rendering and automatic browser refreshes as you make changes. This is ideal for iterative documentation development. ```bash just docs-serve ``` -------------------------------- ### Perform Basic MongoDB Operations Source: https://github.com/mongodb/mongo-python-driver/blob/master/README.md A basic example demonstrating how to connect to a local MongoDB instance, insert documents, query data, and manage indexes. ```pycon >>> import pymongo >>> client = pymongo.MongoClient("localhost", 27017) >>> db = client.test >>> db.name 'test' >>> db.my_collection Collection(Database(MongoClient('localhost', 27017), 'test'), 'my_collection') >>> db.my_collection.insert_one({"x": 10}).inserted_id ObjectId('4aba15ebe23f6b53b0000000') >>> db.my_collection.insert_one({"x": 8}).inserted_id ObjectId('4aba160ee23f6b543e000000') >>> db.my_collection.insert_one({"x": 11}).inserted_id ObjectId('4aba160ee23f6b543e000002') >>> db.my_collection.find_one() {'x': 10, '_id': ObjectId('4aba15ebe23f6b53b0000000')} >>> for item in db.my_collection.find(): ... print(item["x"]) ... 10 8 11 >>> db.my_collection.create_index("x") 'x_1' >>> for item in db.my_collection.find().sort("x", pymongo.ASCENDING): ... print(item["x"]) ... 8 10 11 >>> [item["x"] for item in db.my_collection.find().limit(2).skip(1)] [8, 11] ``` -------------------------------- ### PEP 723 Inline Metadata Example Source: https://github.com/mongodb/mongo-python-driver/blob/master/integration_tests/README.md Example header for a script using PEP 723 inline metadata to declare dependencies and Python version requirements. ```python # /// script # dependencies = [ # "uvloop>=0.18" # ] # requires-python = ">=3.10" # /// ``` -------------------------------- ### Run PyOpenSSL Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute tests using PyOpenSSL. This involves starting the server, setting up the default sync PyOpenSSL test environment, and then running the tests. ```bash just run-server just setup-tests default_sync pyopenssl just run-tests ``` -------------------------------- ### Install PyMongo Optional Dependencies Source: https://github.com/mongodb/mongo-python-driver/blob/master/README.md Commands to install specific feature sets for PyMongo, such as authentication, encryption, and compression. ```bash python -m pip install "pymongo[gssapi]" ``` ```bash python -m pip install "pymongo[aws]" ``` ```bash python -m pip install "pymongo[ocsp]" ``` ```bash python -m pip install "pymongo[snappy]" ``` ```bash python -m pip install "pymongo[zstd]" ``` ```bash python -m pip install "pymongo[encryption]" ``` ```bash python -m pip install "pymongo[gssapi,aws,ocsp,snappy,zstd,encryption]" ``` -------------------------------- ### Run Search Index Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute Search Index tests. This requires starting the server with the search index configuration, setting up the test environment, and then running the tests. ```bash just run-server search_index just setup-tests search_index just run-tests ``` -------------------------------- ### Async Test Client Setup and Ping Command Source: https://github.com/mongodb/mongo-python-driver/blob/master/integration_tests/README.md Demonstrates setting up an asynchronous PyMongo client for testing and executing a 'ping' command. Ensure PyMongo is available in the parent directory for this test. ```python import asyncio import sys from pathlib import Path # Use pymongo from parent directory. root = Path(__file__).parent.parent sys.path.insert(0, str(root)) from test.asynchronous import async_simple_test_client # noqa: E402 async def main(): async with async_simple_test_client() as client: result = await client.admin.command("ping") assert result["ok"] asyncio.run(main()) ``` -------------------------------- ### Install py-spy Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Install the py-spy tool, which is used for CPU profiling of Python applications. This is a prerequisite for generating flame graphs. ```bash pip install py-spy ``` -------------------------------- ### Handle ServerSelectionTimeoutError Examples Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Common error messages encountered when directConnection settings are misconfigured during migration. ```default pymongo.errors.ServerSelectionTimeoutError: mongo_node2: [Errno 8] nodename nor servname provided, or not known,mongo_node1:27017 ``` ```default ServerSelectionTimeoutError: No servers match selector "Primary()", Timeout: 30s, Topology Description: ... ``` -------------------------------- ### Run SSL Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute tests with TLS enabled on the server. This involves starting the server with SSL, setting up the test environment with SSL, and then running the tests. ```bash just run-server --ssl just setup-tests --ssl just run-tests ``` -------------------------------- ### Run PyMongo Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/README.md Commands to install test dependencies and execute the test suite. ```bash pip install -e ".[test]" pytest ``` -------------------------------- ### Check Python Version Source: https://github.com/mongodb/mongo-python-driver/blob/master/README.md Use this command to verify the exact Python version and patch level installed. ```bash python -c "import sys; print(sys.version)" ``` -------------------------------- ### Run Encryption Tests with PyOpenSSL Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute encryption tests specifically configured to use PyOpenSSL. This involves starting the server, setting up the encryption test environment with PyOpenSSL, and then running the tests. ```bash just run-server just setup-tests encryption pyopenssl just run-tests ``` -------------------------------- ### Run mod_wsgi Replica Set Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute mod_wsgi tests for a replica set topology. This requires an Evergreen Linux host with the Python toolchain. The server must be running before setup. ```bash TOPOLOGY=replica_set just run-server just setup-tests mod_wsgi standalone just run-tests ``` -------------------------------- ### Configure Python Version for Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Specify Python versions for test tasks using TOOLCHAIN_VERSION or UV_PYTHON. TOOLCHAIN_VERSION is for toolchain-enabled variants, while UV_PYTHON is for direct binary specification or when toolchains are unavailable. If neither is set, UV installs the minimum supported Python. ```bash TOOLCHAIN_VERSION=3.10 ``` ```bash UV_PYTHON=/path/to/python ``` ```bash UV_PYTHON=3.11 ``` -------------------------------- ### Run OCSP Tests with Valid Delegate Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute OCSP tests specifically for the 'valid-delegate' scenario. Ensure the orchestration file is exported and the mock OCSP responder is started before the server. ```bash export ORCHESTRATION_FILE=rsa-basic-tls-ocsp-disableStapling.json just setup-tests ocsp valid-delegate just run-tests ``` -------------------------------- ### Run mod_wsgi Tests in Standalone Mode Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute mod_wsgi tests in standalone mode. This requires an Evergreen Linux host with the Python toolchain. The server must be running before setup. ```bash just run-server just setup-tests mod_wsgi standalone just run-tests ``` -------------------------------- ### Convert Synchronous Test to Async Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Use the convert_test_to_async.py script to generate a partially converted asynchronous version of an existing synchronous test file. This generated file serves as a starting point for manual completion. ```bash python tools/convert_test_to_async.py [test_file.py] ``` -------------------------------- ### Run OCSP Tests with Valid Certificate Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute OCSP tests specifically for the 'valid' certificate scenario. Ensure the orchestration file is exported and the mock OCSP responder is started before the server. ```bash export ORCHESTRATION_FILE=rsa-basic-tls-ocsp-disableStapling.json just setup-tests ocsp valid just run-tests ``` -------------------------------- ### Run Local KMS Tests with Azure Fail Scenario Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute local KMS tests configured for Azure, expecting failure. This involves starting the server, setting up the test environment, and then running the tests. ```bash just run-server just setup-tests kms azure-fail just run-tests ``` -------------------------------- ### Run OCSP Tests with Revoked Delegate Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute OCSP tests specifically for the 'revoked-delegate' scenario. Ensure the orchestration file is exported and the mock OCSP responder is started before the server. ```bash export ORCHESTRATION_FILE=rsa-basic-tls-ocsp-disableStapling.json just setup-tests ocsp revoked-delegate just run-tests ``` -------------------------------- ### Compile mod_wsgi for Python 2.7 Source: https://github.com/mongodb/mongo-python-driver/blob/master/test/mod_wsgi_test/README.rst Compile mod_wsgi with a specific Python interpreter using the --with-python flag. This example shows compilation for Python 2.7 on a RedHat-like system. ```bash sudo yum install -y httpd httpd-devel wget https://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz tar xzf mod_wsgi-3.4.tar.gz cd mod_wsgi-3.4 ./configure --with-python=/some/directory/bin/python LDFLAGS="-Wl,--rpath=/some/directory/lib" make make install ``` -------------------------------- ### Run mod_wsgi Tests in Embedded Mode Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute mod_wsgi tests in embedded mode. This requires an Evergreen Linux host with the Python toolchain. The server must be running before setup. ```bash just run-server just setup-tests mod_wsgi embedded just run-tests ``` -------------------------------- ### Run OCSP Tests with Revoked Certificate Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute OCSP tests specifically for the 'revoked' certificate scenario. Ensure the orchestration file is exported and the mock OCSP responder is started before the server. ```bash export ORCHESTRATION_FILE=rsa-basic-tls-ocsp-disableStapling.json just setup-tests ocsp revoked just run-tests ``` -------------------------------- ### Run MockupDB Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set up and run MockupDB tests. This involves configuring the test environment and then executing the tests. ```bash just setup-tests mockupdb just run-tests ``` -------------------------------- ### Run Tests with Minimum Dependencies Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Pass the '--test-min-deps' flag to 'just setup-tests' to execute test suites using the minimum supported dependencies. This ensures compatibility with older environments. ```bash just setup-tests --test-min-deps ``` -------------------------------- ### Run Performance Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Commands to set up and run performance tests. Ensure the appropriate server version is running before execution. ```bash just run-server --version=v8.0-perf --ssl ``` ```bash just setup-tests perf sync ``` ```bash just run-tests ``` -------------------------------- ### Run Atlas Connect Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set up and run tests for Atlas Connect. This involves setting up the test environment and then executing the tests. ```bash just setup-tests atlas_connect just run-tests ``` -------------------------------- ### Build Documentation Locally Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Compiles the project's documentation locally using the 'just docs' command. This allows you to preview documentation changes before committing them. ```bash just docs ``` -------------------------------- ### Run Manual Linting with Just Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Triggers a manual linting process for the entire project using the 'just' command. This is a convenient way to run all configured linters. ```bash just run lint-manual ``` -------------------------------- ### Create User with Database.command() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the createUser command within Database.command() to create new users with specified roles. ```python db.command("createUser", "admin", pwd="password", roles=["dbAdmin"]) ``` ```python db.command("createUser", "user", pwd="password", roles=["read"]) ``` -------------------------------- ### Run AWS Lambda Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set up and run AWS Lambda tests. This requires specific AWS role access, detailed in the project's Wiki. Ensure the test environment is set up before running the tests. ```bash just setup-tests aws_lambda just run-tests ``` -------------------------------- ### Replace initialize_unordered_bulk_op() with bulk_write(ordered=False) Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use `Collection.bulk_write()` with `ordered=False` instead of `initialize_unordered_bulk_op()`. ```python batch = coll.initialize_unordered_bulk_op() batch.insert({'a': 1}) batch.find({'a': 1}).update_one({'$set': {'b': 1}}) batch.find({'a': 2}).upsert().replace_one({'b': 2}) batch.find({'a': 3}).remove() result = batch.execute() ``` ```python coll.bulk_write([ InsertOne({'a': 1}), UpdateOne({'a': 1}, {'$set': {'b': 1}}), ReplaceOne({'a': 2}, {'b': 2}, upsert=True), DeleteOne({'a': 3}), ], ordered=False) ``` -------------------------------- ### Replace Database.profiling_info() with find() on system.profile Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Query the 'system.profile' collection directly using find() to retrieve profiling information. ```python profiling_info = db.profiling_info() ``` ```python profiling_info = list(db['system.profile'].find()) ``` -------------------------------- ### Replace initialize_ordered_bulk_op() with bulk_write() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use `Collection.bulk_write()` with a list of write operations instead of `initialize_ordered_bulk_op()` and `BulkOperationBuilder`. ```python batch = coll.initialize_ordered_bulk_op() batch.insert({'a': 1}) batch.find({'a': 1}).update_one({'$set': {'b': 1}}) batch.find({'a': 2}).upsert().replace_one({'b': 2}) batch.find({'a': 3}).remove() result = batch.execute() ``` ```python coll.bulk_write([ InsertOne({'a': 1}), UpdateOne({'a': 1}, {'$set': {'b': 1}}), ReplaceOne({'a': 2}, {'b': 2}, upsert=True), DeleteOne({'a': 3}), ]) ``` -------------------------------- ### Retrieve server limits via hello command Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Replaces direct MongoClient properties with the authoritative values returned by the hello command. ```python max_bson_size = client.max_bson_size max_message_size = client.max_message_size max_write_batch_size = client.max_write_batch_size ``` ```python doc = client.admin.command('hello') max_bson_size = doc['maxBsonObjectSize'] max_message_size = doc['maxMessageSizeBytes'] max_write_batch_size = doc['maxWriteBatchSize'] ``` -------------------------------- ### Run Doc Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute documentation tests. These tests require a running MongoDB server. Ensure the server is running, the doc test environment is set up, and then run the tests. ```bash just run-server just setup-tests doctest just run-tests ``` -------------------------------- ### Run Tests with Coverage and Generate HTML Report Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Executes tests while collecting coverage data and then generates an HTML report. Use 'just test-coverage' to run tests with coverage and 'just coverage-html' to generate the report. ```bash just test-coverage ``` ```bash just coverage-html ``` -------------------------------- ### List database names Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Replace the deprecated database_names method with list_database_names. ```default names = client.database_names() ``` ```default names = client.list_database_names() ``` -------------------------------- ### Check PyMongo Version Source: https://github.com/mongodb/mongo-python-driver/blob/master/README.md Use this command to verify the exact PyMongo version and C extension availability. ```bash python -c "import pymongo; print(pymongo.version); print(pymongo.has_c())" ``` -------------------------------- ### Configure JSONOptions tz_aware behavior Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Demonstrates the default naive datetime decoding and how to restore timezone-aware behavior using JSONOptions. ```python >>> from bson import json_util >>> s = '{"dt": {"$date": "2022-05-09T17:54:00Z"}}' >>> json_util.loads(s) {'dt': datetime.datetime(2022, 5, 9, 17, 54)} ``` ```python >>> from bson import json_util >>> opts = json_util.JSONOptions(tz_aware=True) >>> s = '{"dt": {"$date": "2022-05-09T17:54:00Z"}}' >>> json_util.loads(s, json_options=opts) {'dt': datetime.datetime(2022, 5, 9, 17, 54, tzinfo=)} ``` -------------------------------- ### Run Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute the test suite within the configured Python environment. ```bash just run-tests ``` -------------------------------- ### Run Memory Profiling with memray Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Run memray to profile memory usage, including native code extensions. Use the --native flag and specify an output file for the binary data. This command should be run on a Linux system. ```bash python -m memray run --native -o test.bin ``` -------------------------------- ### Run PyOpenSSL Async Tests with Fallback Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Verify PyMongo's fallback to the standard library OpenSSL in async tests when PyOpenSSL is specified. This involves setting up the async PyOpenSSL test environment and running the tests. ```bash just setup-tests default_async pyopenssl just run-tests ``` -------------------------------- ### Access client configuration options Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Replaces direct configuration helper properties on MongoClient with access through the options attribute. ```python client.event_listeners client.local_threshold_ms client.server_selection_timeout client.max_pool_size client.min_pool_size client.max_idle_time_ms ``` ```python client.options.event_listeners client.options.local_threshold_ms client.options.server_selection_timeout client.options.pool_options.max_pool_size client.options.pool_options.min_pool_size client.options.pool_options.max_idle_time_seconds ``` -------------------------------- ### Run AWS Auth Tests with Specific Type Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set up and run AWS authentication tests, specifying the AWS test type. The server must be running before setting up the tests. ```bash just run-server auth_aws just setup-tests auth_aws just run-tests ``` -------------------------------- ### Set Profiling Level with command('profile') Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the command('profile', level, filter) method to set the profiling level and optionally apply a filter. ```python db.set_profiling_level(pymongo.ALL, filter={'op': 'query'}) ``` ```python res = db.command('profile', 2, filter={'op': 'query'}) ``` -------------------------------- ### Require hint with min/max queries Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md The hint option is now mandatory when using min or max filters to ensure index usage. ```python cursor = coll.find({}, min={'x', min_value}) ``` ```python cursor = coll.find({}, min={'x', min_value}, hint=[('x', ASCENDING)]) ``` -------------------------------- ### Run Synchro Hook Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Run the synchro pre-commit hook to generate the latest version of the synchronous code from asynchronous code. This should be done before running tests. ```bash pre-commit run --all-files synchro ``` -------------------------------- ### Replace Database.profiling_level() with command('profile') Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Retrieve profiling information using the command('profile', -1) method, then access the 'was' field for the previous level. ```python level = db.profiling_level() ``` ```python profile = db.command('profile', -1) level = profile['was'] ``` -------------------------------- ### Run Integration Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute the integration tests suite, which verifies PyMongo usage with downstream packages and frameworks. These tests should function correctly with or without SSL. ```bash just integration_tests ``` -------------------------------- ### Run Free-threaded Python Tests Locally Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute tests using a specific free-threaded Python version locally. Ensure the server is running and tests are set up before running this command. ```bash UV_PYTHON=3.14t just run-tests ``` -------------------------------- ### Run Specific Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Allows running specific tests by providing a file path, class name, or test name. This is useful for focusing on a particular area of the codebase during development. ```bash just test test/test_change_stream.py::TestUnifiedChangeStreamsErrors::test_change_stream_errors_on_ElectionInProgress ``` -------------------------------- ### Drop User with Database.command() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the dropUser command within Database.command() to remove a user. ```python db.command("dropUser", "user") ``` -------------------------------- ### Enable Deprecation Warnings Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use these commands to enable or promote deprecation warnings to errors during application execution. ```default python -Wd ``` ```default python -Wd -Werror ``` -------------------------------- ### Record CPU Profile with py-spy Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Record a CPU profile using py-spy to generate a flame graph. Specify the output file, sample rate, and the Python script to profile. Note that sudo is required on macOS. ```bash py-spy record -o -r -- python ``` -------------------------------- ### Generate Flamegraph with memray Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Generate an HTML flamegraph from memray's binary output. This visualizes memory allocation patterns and helps identify potential leaks. Refer to the memray documentation for additional options. ```bash python -m memray flamegraph -o test.html test.bin ``` -------------------------------- ### Run Specific Pre-commit Hook Manually Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Allows manual execution of a specific linting hook, such as 'ruff', across all files. Use this for targeted checks or when you only need to run a particular linter. ```bash pre-commit run --all-files --hook-stage manual ruff ``` -------------------------------- ### PyMongo Async Modules Overview Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/api/pymongo/asynchronous/index.md Overview of the primary sub-modules available in the pymongo async driver for MongoDB operations. ```APIDOC ## PyMongo Async Modules ### Description The pymongo async driver provides several sub-modules to handle MongoDB operations asynchronously. ### Modules - **change_stream**: Watch changes on a collection, database, or cluster. - **client_session**: Logical sessions for sequential operations. - **collection**: Collection level operations. - **command_cursor**: Tools for iterating over MongoDB command results. - **cursor**: Tools for iterating over MongoDB query results. - **database**: Database level operations. - **mongo_client**: Tools for connecting to MongoDB. ``` -------------------------------- ### Run Enterprise Auth Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute Enterprise Authentication tests. These tests can only be run from an Evergreen host. Ensure the server is running and the test environment is set up. ```bash just run-server enterprise_auth just setup-tests enterprise_auth just run-tests ``` -------------------------------- ### Run OIDC Auth Tests with GCP Type Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set up and run OIDC authentication tests for the GCP type. Ensure you have the necessary AWS credentials configured if using related services. ```bash just setup-tests auth_oidc gcp just run-tests ``` -------------------------------- ### Run All Tests Locally Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Executes all tests in the project using the 'just test' command or directly with 'pytest'. Ensure your MongoDB server is running before executing tests. ```bash just test ``` ```bash pytest ``` -------------------------------- ### Execute fsyncUnlock command Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the admin command method to unlock the database after the removal of the dedicated unlock method. ```default client.admin.command('fsyncUnlock') ``` -------------------------------- ### Execute fsync command Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the admin command method to perform an fsync operation after the removal of the dedicated fsync method. ```default client.admin.command('fsync', lock=True) ``` -------------------------------- ### Run Serial mod_wsgi Test Client Source: https://github.com/mongodb/mongo-python-driver/blob/master/test/mod_wsgi_test/README.rst Execute the test_client.py script to perform serial HTTP requests to Apache. The -n argument specifies the total number of requests. ```bash python test/mod_wsgi_test/test_client.py -n 25000 serial \ http://localhost/interpreter1${WORKSPACE} http://localhost/interpreter2${WORKSPACE} ``` -------------------------------- ### List collections using list_collection_names Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Replaces the removed Database.collection_names method with list_collection_names. ```python names = client.db.collection_names() non_system_names = client.db.collection_names(include_system_collections=False) ``` ```python names = client.db.list_collection_names() non_system_names = client.db.list_collection_names(filter={"name": {"$regex": "^(?!system\\.)"}}) ``` -------------------------------- ### Replace UUIDLegacy with Binary.from_uuid() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md The bson.binary.UUIDLegacy class is removed. Use bson.binary.Binary.from_uuid() for creating UUID objects. ```default uu = uuid.uuid4() uuid_legacy = UUIDLegacy(uu) ``` ```default uu = uuid.uuid4() uuid_legacy = Binary.from_uuid(uu, PYTHON_LEGACY) ``` -------------------------------- ### Run Parallel mod_wsgi Test Client Source: https://github.com/mongodb/mongo-python-driver/blob/master/test/mod_wsgi_test/README.rst Execute the test_client.py script to perform parallel HTTP requests to Apache. The -n argument specifies the total number of requests, and -t specifies the number of threads. Multiple URLs correspond to different sub interpreters. ```bash python test/mod_wsgi_test/test_client.py -n 2500 -t 100 parallel \ http://localhost/interpreter1${WORKSPACE} http://localhost/interpreter2${WORKSPACE} ``` -------------------------------- ### Run OIDC Auth Tests with EKS Type Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set up and run OIDC authentication tests for the EKS type. This requires specific AWS role access, detailed in the project's Wiki. ```bash just setup-tests auth_oidc eks just run-tests ``` -------------------------------- ### Migrate ensure_index to create_index Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Avoid frequent index creation by checking for existence, as create_index does not maintain an internal cache like ensure_index. ```python def persist(self, document): collection.ensure_index('a', unique=True) collection.insert_one(document) ``` ```python def persist(self, document): if not self.created_index: collection.create_index('a', unique=True) self.created_index = True collection.insert_one(document) ``` -------------------------------- ### Migrate reindex to database command Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Replace the removed reindex method with the database command interface. ```python >>> result = database.my_collection.reindex() ``` ```python >>> result = database.command('reIndex', 'my_collection') ``` -------------------------------- ### Select Tests by Pattern Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Uses the '-k' argument with 'pytest' to select and run tests that match a specified pattern. This provides flexibility in targeting tests based on names or keywords. ```bash pytest -k "test_pattern" ``` -------------------------------- ### Replace Database.current_op() with aggregate() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the aggregate() method with the $currentOp stage to retrieve current operations instead of the removed Database.current_op(). ```python ops = client.admin.current_op()['inprog'] ``` ```python ops = list(client.admin.aggregate([{'$currentOp': {}}])) ``` -------------------------------- ### Compile Python for Shared Library Source: https://github.com/mongodb/mongo-python-driver/blob/master/test/mod_wsgi_test/README.rst Compile Python from source with the --enable-shared flag to create a shared library, necessary for mod_wsgi integration. Ensure LDFLAGS includes --rpath for library linking. ```bash ./configure --prefix=/some/directory --enable-shared LDFLAGS="-Wl,--rpath=/some/directory/lib" make make install ``` -------------------------------- ### Clone Drivers Evergreen Tools Repository Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Clones the 'drivers-evergreen-tools' repository, which contains tools and scripts necessary for running tests that require specific configurations or services. ```bash git clone git@github.com:mongodb-labs/drivers-evergreen-tools.git ``` -------------------------------- ### Check isWritablePrimary Attribute Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Verify the isWritablePrimary attribute returned by the hello command when directConnection is set to False. ```default >>> client.admin.command('hello')['isWritablePrimary'] True ``` -------------------------------- ### Stop Server Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Terminate the running MongoDB server instance. ```bash just stop-server ``` -------------------------------- ### Run Mypy Type Checking with Just Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Executes the mypy type checker using the 'just' command. This helps ensure type safety within the codebase. ```bash just typing-mypy ``` -------------------------------- ### Run Pre-commit Linters Manually Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Executes all pre-commit checks across the entire codebase. This is useful for a comprehensive linting pass before submitting changes. ```bash pre-commit run --all-files ``` -------------------------------- ### Replace Collection.count() and Cursor.count() with count_documents() or estimated_document_count() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use `count_documents()` for queries or `estimated_document_count()` for total document counts instead of the removed `Collection.count()` and `Cursor.count()` methods. ```python ntotal = collection.count({}) nmatched = collection.count({'price': {'$gte': 10}}) # Or via the Cursor.count api: ntotal = collection.find({}).count() nmatched = collection.find({'price': {'$gte': 10}}).count() ``` ```python ntotal = collection.estimated_document_count() nmatched = collection.count_documents({'price': {'$gte': 10}}) ``` -------------------------------- ### Regenerate Test Variants and Tasks Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Run this command to regenerate test variants and tasks after making changes to configuration files. This ensures that the test infrastructure is up-to-date. ```bash pre-commit run --all-files generate-config ``` -------------------------------- ### Run Remote KMS Tests with GCP Pass Scenario Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Execute remote KMS tests configured for GCP, expecting success. This involves setting up the test environment and then running the tests. ```bash just setup-tests kms gcp just run-tests ``` -------------------------------- ### Replace SON.iteritems() with SON.items() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md The SON.iteritems() method has been removed. Use SON.items() instead for iterating over key-value pairs. ```default for k, v in son.iteritems(): ``` ```default for k, v in son.items(): ``` -------------------------------- ### Enable Debug Logs for Evergreen Patch Builds Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Use this command to enable debug logs for failed tests in Evergreen patch builds. This helps in debugging issues specific to the patch environment. ```bash evergreen patch --param DEBUG_LOG=1 ``` -------------------------------- ### Update User with Database.command() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the updateUser command within Database.command() to change a user's password or roles. ```python db.command("updateUser", "user", pwd="newpassword") ``` ```python db.command("updateUser", "user", roles=["readWrite"]) ``` -------------------------------- ### Regex Module Overview Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/api/bson/regex.md Overview of the regex module introduced in version 2.7 for MongoDB regular expression representation. ```APIDOC ## Regex Module ### Description Tools for representing MongoDB regular expressions. ### Version Added in version 2.7. ``` -------------------------------- ### Replace Collection.find_and_modify() with newer find_one_ methods Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use `find_one_and_update()`, `find_one_and_replace()`, or `find_one_and_delete()` instead of the removed `Collection.find_and_modify()` method. ```python updated_doc = collection.find_and_modify({'a': 1}, {'$set': {'b': 1}}) replaced_doc = collection.find_and_modify({'b': 1}, {'c': 1}) deleted_doc = collection.find_and_modify({'c': 1}, remove=True) ``` ```python updated_doc = collection.find_one_and_update({'a': 1}, {'$set': {'b': 1}}) replaced_doc = collection.find_one_and_replace({'b': 1}, {'c': 1}) deleted_doc = collection.find_one_and_delete({'c': 1}) ``` -------------------------------- ### Initialize Google Tag Manager Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/_templates/layout.html This JavaScript snippet initializes Google Tag Manager. It should be included in the head of your HTML document to enable tracking and analytics. ```javascript (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-GDFN'); ``` -------------------------------- ### Set MOD_WSGI_SO Environment Variable Source: https://github.com/mongodb/mongo-python-driver/blob/master/test/mod_wsgi_test/README.rst Export the MOD_WSGI_SO environment variable to specify the path to the compiled mod_wsgi.so library, allowing Apache configuration to locate it. ```bash export MOD_WSGI_SO=/path/to/mod_wsgi.so ``` -------------------------------- ### Migrate find modifiers to direct parameters Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md The modifiers parameter is removed; pass query options directly as keyword arguments to find methods. ```python cursor = coll.find({}, modifiers={ "$comment": "comment", "$hint": {"_id": 1}, "$min": {"_id": 0}, "$max": {"_id": 6}, "$maxTimeMS": 6000, "$returnKey": False, "$showDiskLoc": False, }) ``` ```python cursor = coll.find( {}, comment="comment", hint={"_id": 1}, min={"_id": 0}, max={"_id": 6}, max_time_ms=6000, return_key=False, show_record_id=False, ) ``` -------------------------------- ### Replace Collection.insert() with insert_one() or insert_many() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use insert_one() for single documents and insert_many() for multiple documents instead of the removed Collection.insert(). ```python collection.insert({'doc': 1}) ``` ```python collection.insert([{'doc': 2}, {'doc': 3}]) ``` ```python collection.insert_one({'my': 'document'}) ``` ```python collection.insert_many([{'doc': 2}, {'doc': 3}]) ``` -------------------------------- ### Enable Debug Logs with Pytest Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Use these options with 'just test' or 'pytest' to output all debug logs to the terminal. Warning: this generates a large volume of logs. ```bash just test -o log_cli_level="DEBUG" -o log_cli=1 ``` ```bash pytest -o log_cli_level="DEBUG" -o log_cli=1 ``` -------------------------------- ### Run Specification Test Resync Scripts Locally Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Executes the specification test resynchronization scripts locally without generating a pull request. Supports both shell script and Python script execution. ```bash ./.evergreen/scripts/resync-all-specs.sh ``` ```python python3 ./.evergreen/scripts/resync-all-specs.py ``` -------------------------------- ### Enable Default Debug Logs in pyproject.toml Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Configure pyproject.toml to enable debug logs by default for your machine. This setting affects all subsequent test runs. ```toml [tool.pytest.ini_options] log_cli=1 log_cli_level="DEBUG" ``` -------------------------------- ### Implement custom file digests with GridFSBucket Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md The disable_md5 option for GridFSBucket and GridFS is removed as GridFS no longer generates checksums. Implement custom digests like SHA256 outside of GridFS and store them as metadata. ```default import hashlib my_db = MongoClient().test fs = GridFSBucket(my_db) with fs.open_upload_stream("test_file") as grid_in: file_data = b'...' # Replace with actual file data sha356 = hashlib.sha256(file_data).hexdigest() grid_in.write(file_data) grid_in.sha356 = sha356 # Set the custom 'sha356' field ``` -------------------------------- ### Resync Specification Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Clones the specifications repository and resynchronizes PyMongo's copy of the tests. Use the -b flag to exclude specific files or directories from the update. ```bash git clone git@github.com:mongodb/specifications.git export MDB_SPECS=~/specifications cd ~/mongo-python-driver/.evergreen ./resync-specs.sh -b "" spec1 spec2 ... ./resync-specs.sh -b "connection-string*" crud bson-corpus # Updates crud and bson-corpus specs while ignoring all files with the regex "connection-string*" cd .. ``` -------------------------------- ### Connect to MongoDB with TLS and Invalid Certificates Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Use this Python code to establish a connection to a MongoDB server with TLS enabled, allowing invalid certificates. This is intended for local testing purposes only. ```python from pymongo import MongoClient client = MongoClient( "mongodb://localhost:27017?tls=true&tlsAllowInvalidCertificates=true" ) ``` -------------------------------- ### Check database lock status Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use the currentOp command to check for fsync locks as a replacement for the removed is_locked property. ```default is_locked = client.admin.command('currentOp').get('fsyncLock') ``` -------------------------------- ### Implement Collection.save() functionality Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md A custom save function using replace_one for existing documents or insert_one for new documents. ```python def save(doc): if '_id' in doc: collection.replace_one({'_id': doc['_id']}, doc, upsert=True) return doc['_id'] else: res = collection.insert_one(doc) return res.inserted_id ``` -------------------------------- ### Set Drivers Tools Environment Variable Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Sets the DRIVERS_TOOLS environment variable to the path of the cloned 'drivers-evergreen-tools' repository. This is a prerequisite for running certain tests that rely on these tools. ```bash export DRIVERS_TOOLS=$PWD/drivers-evergreen-tools ``` -------------------------------- ### Copy Flamegraph from Host Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Copy the generated flamegraph HTML file from a remote host (e.g., an EC2 instance) to the local machine using scp. This allows for local viewing of the memory profile. ```bash scp ubuntu@ec2-3-82-52-49.compute-1.amazonaws.com:/home/ubuntu/test.html . ``` -------------------------------- ### Authenticate using separate MongoClient instances Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Replaces deprecated Database.authenticate calls with individual MongoClient instances per user. ```python client = MongoClient() client.admin.authenticate('user1', 'pass1') client.admin.authenticate('user2', 'pass2') ``` ```python client1 = MongoClient(username='user1', password='pass1') client2 = MongoClient(username='user2', password='pass2') ``` -------------------------------- ### Replace Collection.save() with update_one() or insert_one() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use update_one() for updates and insert_one() for new documents to replace the functionality of the removed Collection.save(). ```python doc = collection.find_one({"_id": "some id"}) doc["some field"] = db.collection.save(doc) ``` ```python result = collection.update_one({"_id": "some id"}, {"$set": {"some field": }}) ``` -------------------------------- ### Replace Collection.update() with update_one() or update_many() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use update_one() for single document updates and update_many() for multiple document updates instead of the removed Collection.update(). ```python collection.update({}, {'$set': {'a': 1}}) ``` ```python collection.update({}, {'$set': {'b': 1}}, multi=True) ``` ```python collection.update_one({}, {'$set': {'a': 1}}) ``` ```python collection.update_many({}, {'$set': {'b': 1}}) ``` -------------------------------- ### Specify projection for _id field Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Empty projections now return the entire document; explicitly set the projection to include only the _id field if that is the desired behavior. ```python coll.find({}, projection={}) ``` ```python coll.find({}, projection={"_id":1}) ``` -------------------------------- ### Tear Down Test Environment Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Clean up the test environment after tests have been completed. ```bash just teardown-tests ``` -------------------------------- ### Replace Database.eval with database.command('eval') Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md The Database.eval() method is removed. For MongoDB versions <= 4.0, use database.command('eval', Code(...)) to execute JavaScript functions. ```default result = database.eval('function (x) {return x;}', 3) ``` ```default from bson.code import Code result = database.command('eval', Code('function (x) {return x;}'), args=[3]).get('retval') ``` -------------------------------- ### Enable Debug Logs for Failed Tests Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set the DEBUG_LOG environment variable to enable debug logs specifically for failed tests. This is useful for diagnosing issues without excessive output. ```bash DEBUG_LOG=1 just setup-tests ``` ```bash DEBUG_LOG=1 just-test ``` ```bash DEBUG_LOG=1 pytest ``` -------------------------------- ### Enable Flaky Decorator Locally Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Set the ENABLE_FLAKY environment variable to enable the 'flaky' decorator when running tests locally. This is disabled by default outside of CI. ```bash ENABLE_FLAKY=1 ``` -------------------------------- ### Check for Database existence Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Explicitly check if a database object is not None, as evaluating it as a boolean now raises NotImplementedError. ```python if database: ``` ```python if database is not None: ``` -------------------------------- ### Replace Collection.remove() with delete_one() or delete_many() Source: https://github.com/mongodb/mongo-python-driver/blob/master/doc/migrate-to-pymongo4.md Use `delete_one()` to remove a single document or `delete_many()` to remove multiple documents instead of the removed `Collection.remove()` method. ```python collection.remove({'a': 1}, multi=False) collection.remove({'b': 1}) ``` ```python collection.delete_one({'a': 1}) collection.delete_many({'b': 1}) ``` -------------------------------- ### Add Spec Test Changes to Patch File Source: https://github.com/mongodb/mongo-python-driver/blob/master/CONTRIBUTING.md Appends desired spec test changes to a patch file for a specific PYTHON ticket. This is used for unimplemented features or bug fixes. ```bash git diff HEAD~1 path/to/file >> .evergreen/spec-patch/PYTHON-1234.patch ```