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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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