pythonmongodbazure-cosmosdb-mongoapi

Silence pymongo "You appear to be connected to a CosmosDB cluster" warning


Any time I connect to our Azure-hosted CosmosDB Mongo database, we are given the following warning:

UserWarning: You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb

(mongo-python-driver GitHub source line for reference)

Nothing seems to broken by ignoring the warning and continuing, however running our test suite is now plagued with this in our CI output's STDOUT upon every execution with an otherwise pristine output.

Why does this show as a warning when nothing is wrong? Does the mongo driver provide a way of disabling the message without silencing genuine warnings?


Solution

  • UserWarning: You appear to be connected to a CosmosDB cluster.

    pymongo indicates that it has identified a Cosmos DB instance hosted on Azure being utilized as the MongoDB server. The error message is to inform users that Cosmos DB, while MongoDB-compatible, may not support all MongoDB features, especially those available in native MongoDB clusters. You can use warnings module to ignore the warnings. Below is an example on how you can ignore warnings.

    import warnings
    from pymongo import MongoClient
    
    warnings.filterwarnings("ignore", message="You appear to be connected to a CosmosDB cluster")
    
    client = MongoClient("<connection_string>")
    db = client.get_database("newdb1")
    
    print(db.list_collection_names())
    

    Output:

    ['newcoll1']