import pymongo
from pymongo import MongoClient
from msticpy.data import data_obfus
import json
import sys
# Database: learninigLocker
# Collection: users
# Connect to MongoDB
client = MongoClient('mongodb://<username>:<password>@<ip_address>:27017/admin')
db = client["test"]
collection = db["payments"]
# Define masking function
def mask_data(data):
hashed_data = data.copy()
if 'card_num' in hashed_data:
hashed_data['card_num'] = data_obfus.hash_string(hashed_data['card_num'])
if 'card_name' in hashed_data:
hashed_data['card_name'] = data_obfus.hash_item(hashed_data['card_name'], delim=' ')
return hashed_data
# Iterate over the collection and apply the masking function
for document in collection.find():
masked_document = mask_data(document)
collection.replace_one({'_id': document['_id']}, masked_document)
# Close the MongoDB connection
client.close()