Running commands with Pymongo on Admin database

Hey :wave: ,

I’m trying to add the expireAfterSeconds parameter to our MongoDB instance. The full command i’m trying to run is…

import pymongo
client = pymongo.MongoClient(mongo_db_connection_string)
db = client[database_name]

result = db.command(
    {
        'setClusterParameter': {
            'changeStreamOptions': {
                'preAndPostImages': {
                    'expireAfterSeconds': expire_after_seconds
                }
            }
        }
    }
)

But I get this error…

OperationFailure: setClusterParameter may only be run against the admin database.

Then when I try to run it against the Admin database like this…

db = client['admin']

result = db.command(
    {
        'setClusterParameter': {
            'changeStreamOptions': {
                'preAndPostImages': {
                    'expireAfterSeconds': expire_after_seconds
                }
            }
        }
    }
)

I get this error…
not authorized on admin to execute command

So I can’t use the Database role I made (which has Atlas Admin privileges) to run this command, so I need another user/role. I tried authenticating with my own username and password (when I log into the Atlas Gui), and I have full access rights to the MongoDB project, but wasn’t able to run the command either…

import urllib.parse
from pymongo import MongoClient

username = urllib.parse.quote_plus("USERNAME")
password = urllib.parse.quote_plus("PASSWORD")

mongo_uri = f'mongodb+srv://{username}:{password}@CONNECTION_STRING_DETAILS/admin?retryWrites=true&w=majority'
client = MongoClient(mongo_uri)

db = client["admin"]
... etc ...

But got this error…
OperationFailure: Authentication failed., full error:

I seem to only be able to connect to my Project using a Database User, but I am unable to assign ClusterAdmin to any Database Users - so I can’t run any commands on the admin database. So I thought I would connect with my user account that I log into MongoAtlas, but that authentication doesn’t work.

How can I connect to the Atlas database so that I can run setClusterParameter commands?

This post is similar but was not resolved: MongoServerError: not authorized on admin to execute command - #3 by Hannes_Calitz

My question boils down to this:

  • Am I able to run any commands on the admin database using MongoSH for a cluster hosted on MongoDB Atlas? My exploration seems like this is not possible, and instead to make edits through the gui.

I’m using MongoDB Atlas.

Any help or advice would be great.

Cheers,
Paul

Many commands are unavailable, limited or use another mechanism to change configuration(atlas cli or gui) and this can vary from shared tier to dedicated tier.

I don’t think this list is exhaustive:

Ok thanks @chris , appreciate the response.

The command I want to run is setClusterParameter. In the documentation you shared it doesn’t explicitly say that command is not available on Atlas clusters, but the setParameter is not available. Is it possible that setParameter encompasses setClusterParameter? Seems likely but want to confirm.

Is there any way to manually adjust the setClusterParameter:changeStreamOptions: preAndPostImages: for clusters hosted on Atlas? This is quite important for our use case and seems like a major feature limitation if it’s not possible.

Cheers,
Paul

This one might need an answer from the MongoDB team. What Altlas tier are you on?

Are you specifically trying to change retention period for pre and post images or are you trying to enable pre and post images?

The latter can be done via collMod

Thanks @chris ,

They can be quite hard to get a hold of :sweat_smile:

But yes I’ve already enabled Pre & Post images via collmod, but would like to now set the retention period.

I would happily try to run the setClusterParameter on the DB via mongosh but can’t authenticate with my email and password. I have checked the email and password & url encoded it as documentation mentions - but still no dice. So i’m wondering if it’s even possible to do this (log in using Atlas email and password via mongosh).

Cheers,
Paul