Created
February 26, 2023 09:35
-
-
Save khpraful/710c3b1250c755b7cc5fb8a0003922dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
import sys | |
import json | |
from datetime import datetime | |
def get_database(env, dbName): | |
if env == 'local' : | |
connection_string = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false" | |
client = MongoClient(connection_string) | |
return client[dbName] | |
# Get environment name where changes have to be effected | |
env = sys.argv[1] | |
# Get database name where changes have to be effected | |
dbName = sys.argv[2] | |
# Get collection name in which changes have to be effected | |
collection = sys.argv[3] | |
# Get search criteria (in json format) for finding the original document that needs to be updated | |
searchQuery = sys.argv[4] | |
# Get fields to be updated (in json format) | |
updateFields = sys.argv[5] | |
db = get_database(env, dbName) | |
document = db[collection].find_one(json.loads(searchQuery)) | |
if document is not None: | |
print("============================================================================================") | |
print("Original document:", document) | |
print("============================================================================================") | |
historyDocument = document.copy() | |
del historyDocument['_id'] | |
originId = document['_id'] | |
# Store the original document id in history collection | |
historyDocument['originId'] = originId | |
revision = 0 | |
if 'revision' in document: | |
revision = document['revision'] | |
updatedDate = datetime.now() | |
updateFields = json.loads(updateFields) | |
historyDocument['revision'] = revision | |
historyDocument['updatedDate'] = updatedDate | |
historyDocument['updateDescription'] = updateFields | |
print("============================================================================================") | |
print ("History:", historyDocument) | |
print("============================================================================================") | |
historyCollection = collection + '_history' | |
db[historyCollection].insert_one(historyDocument) | |
# Increment revision number by 1 each time the document is updated | |
updateFields['revision'] = revision + 1 | |
updateFields['updatedDate'] = updatedDate | |
db[collection].update_one(json.loads(searchQuery), {"$set":updateFields}) | |
updatedDocument = db[collection].find_one({'_id': originId}) | |
print("============================================================================================") | |
print("Updated document:", updatedDocument) | |
print("============================================================================================") | |
else: | |
print('Document with search query', searchQuery, 'not found in collection', collection) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment