No project description provided
Project description
MongoDB fields Cleaner
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
- Create a branch and a pull request.
- Label the pull request with the correct semver label: patch, minor, major.
- 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
Built Distribution
Hashes for mdb_fields_cleaner-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59457d770b5463c608f16587e28014e845e87f5c5325d9164134d020c80f31e8 |
|
MD5 | 0e1cac3f773ecf260aa27bf157c678dc |
|
BLAKE2b-256 | fa9546d8fb8f9e06113a23fe6adcd1dea271e0390636a30614346f710f8ed416 |