Skip to main content

Small Python utils to deal with MongoDB.

Project description

MongoUtils

Some utils to deal with MongoDB more easily.

Connect to database

Simple function to connect to MongoDB or a specific database.

Connect to MongoDB server

from monutils import connect
from monutils import Mode

# Connect to localhost in the default port without authentication
client = connect()
# Connect to a host given
client = connect(host, port)
# Using replicaset
client = connect(host, port, replicaset)
# Connect to MongoDB using user and password
client = connect(host, port, replicaset, user, password)
# Without replicaset
client = connect(host, port, None, user, password)
client = connect(host, port, user=user, password=password)
# Select the authentication mechanism (if mode is not set, then it tries all possible authentication methods)
client = connect(host, port, replicaset, user, password, mode=Mode.SCRAM_SHA_256)
# Using certificates
client = connect(host, port, replicaset, user, password, cert_key_file, ca_file, mode=Mode.SCRAM_SHA_256)
# Using session token
client = connect(host, port, replicaset, user, password, session_token=token, mode=Mode.SCRAM_SHA_256)

Connect to MongoDB database

from monutils import connect_database
from monutils import Mode

# Connect to localhost in the default port without authentication
client = connect_database(databasse=database)
# Connect to a host given
client = connect_database(host, port, database=database)
# Using replicaset
client = connect_database(host, port, replicaset, database)
# Connect to MongoDB using user and password
client = connect_database(host, port, replicaset,
                          database, user, password)
# Without replicaset
client = connect_database(host, port, None, database, user, password)
client = connect_database(host, port, 
                          database=database, user=user, password=password)
# Select the authentication mechanism 
# (if mode is not set, then it tries all possible authentication methods)
client = connect_database(host, port, replicaset, database, user, 
                          password, mode=Mode.SCRAM_SHA_256)
# Using certificates
client = connect_database(host, port, replicaset, database, user, password, 
                          cert_key_file, ca_file, mode=Mode.SCRAM_SHA_256)
# Using session token
client = connect_database(host, port, replicaset, database, user, password, 
                          session_token=token, mode=Mode.SCRAM_SHA_256)

Copy collections

You can copy collection from a database to another easily.

from monutils import copy

from_client = connect(...) # Connection with the source MongoDB server
to_client = connect(...) # Connection with the target MongoDB server
# Copy the collection
copy(from_client, to_client, from_db, to_db, from_collection, to_collection)
# Copy de collection ignoring copy errors
copy(from_client, to_client, from_db, to_db, from_collection, to_collection, True)

Create a cache mongodb database

from monutils import connect
from monutils.cache import MongoCache

client = connect(...)
db = client['my_db']
# Create a cache without storage limit
cache = MongoCache(db['my_collection'])
# Add an element to the collection
cache[key] = value
cache.add(key, value)
# Update an existing element to the collection
cache.update(key, value)
# Remove an element from the collection
cache.remove(key)
# Retrieve an element from the collection
cache[key]
# Check if the element is in the collection
key in cache
# Calculate the cache size
cache.size
# Create a cache limited to 1000 elements
cache = MongoCache(db['my_collection'], 1000)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

monutils-0.1.9.tar.gz (8.8 kB view details)

Uploaded Source

File details

Details for the file monutils-0.1.9.tar.gz.

File metadata

  • Download URL: monutils-0.1.9.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for monutils-0.1.9.tar.gz
Algorithm Hash digest
SHA256 229dd1e09b3f3e94ee44939f863e0fb2b06c05b5dac7a453e318c63fe04bf865
MD5 950e4b237ede3b2e18922a2da70423f0
BLAKE2b-256 3e8a73c02029caff46bf362538120ceeb3c608ae5a816a762d7e66de80ea33cb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page