Skip to main content

No project description provided

Project description

MongoDB fields Cleaner

PyPI Version codecov

It simplifies the process of cleaning up old deprecated fields in a MongoDB database.

This will help you to easily remove obsolete/unused fields in your MongoDB collections so that you can save space in your DB.

How to use it

It is as simple as getting a pymongo.MongoClient and pass it to the Cleaner constructor.

Once we have the Cleaner object instance, we just need to call the clean method with the collection name and the field names we want to keep (unset), the fields not in this collection will be removed. This call will return an UpdateResult object.

from mdb_fields_cleaner import Cleaner

client = MongoClient()
cleaner = Cleaner(client)

results: UpdateResult = cleaner.clean(collection.name, ["make", "model"])
print(f"{results.modified_count} modified documents")

Data models

You can use following convenience cleaners to just remove any field in a collection that is not defined in the model.

dataclasses

from pydantic import BaseModel
from mdb_fields_cleaner.dataclass import DataClassCleaner
from dataclasses import dataclass


@dataclass
class User:
    id: int
    name: str


cleaner = DataClassCleaner(MongoClient())
results: UpdateResult = cleaner.clean_fields_not_in_model("users_collection", User)
print(f"{results.modified_count} modified documents")

Pydantic Models

from pydantic import BaseModel
from mdb_fields_cleaner.pydantic import PydanticCleaner


class User(BaseModel):
    id: int
    name: str


cleaner = PydanticCleaner(MongoClient())
results: UpdateResult = cleaner.clean_fields_not_in_model("users_collection", User)
print(f"{results.modified_count} modified documents")

Development flow

  1. Create a branch and a pull request.
  2. Label the pull request with the correct semver label: patch, minor, major.
  3. Get the PR approved and merged.

At this point the package should be published in Pypi.org registry.

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

mdb_fields_cleaner-0.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mdb_fields_cleaner-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file mdb_fields_cleaner-0.2.0.tar.gz.

File metadata

  • Download URL: mdb_fields_cleaner-0.2.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for mdb_fields_cleaner-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3ead50dfb9b8d4c946f00fb0737c4b1e7699446ab3bf3302d22e1b76b48de580
MD5 135b263866a2d93f1c8bc448433ede8d
BLAKE2b-256 07eeef9bb8643f3445760bb88498534fdec2cfb573c8f92e53d4785ecdd16254

See more details on using hashes here.

File details

Details for the file mdb_fields_cleaner-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mdb_fields_cleaner-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59457d770b5463c608f16587e28014e845e87f5c5325d9164134d020c80f31e8
MD5 0e1cac3f773ecf260aa27bf157c678dc
BLAKE2b-256 fa9546d8fb8f9e06113a23fe6adcd1dea271e0390636a30614346f710f8ed416

See more details on using hashes here.

Supported by

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