xxxxxxxxxx
import subprocess
# Run the mongod --version command
command = ['mongod', '--version']
result = subprocess.run(command, capture_output=True, text=True)
# Extract the version information from the result
output = result.stdout.strip()
version = output.split('\n')[0].split(' ')[2]
# Print the MongoDB version
print(f"MongoDB version: {version}")
xxxxxxxxxx
$ mongo --version
MongoDB shell version v5.0.6
Build Info: {
"version": "5.0.6",
"gitVersion": "212a8dbb47f07427dae194a9c75baec1d81d9259",
"modules": [],
"allocator": "system",
"environment": {
"distarch": "x86_64",
"target_arch": "x86_64"
}
}