Skip to content

Instantly share code, notes, and snippets.

@EmberLightVFX
Created July 19, 2023 09:48
Show Gist options
  • Save EmberLightVFX/e47235e6756f6617081610394a570904 to your computer and use it in GitHub Desktop.
Save EmberLightVFX/e47235e6756f6617081610394a570904 to your computer and use it in GitHub Desktop.
Search and replace for OpenPype's MongoDB
import re
from pymongo import MongoClient
# Connect to the MongoDB server
client = MongoClient("mongodb://mongodb://localhost:27017")
# Select the desired database and collection
db = client["avalon"]
collection = db["your-project-name"]
# Select what we should change from -> to
from_str = r"\\publish\\"
to_str = r"\\work\\publish\\"
# Change all "data" and "path" to match the location you wish to search and replace in
documents = collection.find({"data.path": {"$regex": from_str}})
for document in documents:
updated_path = re.sub(from_str, to_str, document["data"]["path"])
result = collection.update_one(
{"_id": document["_id"]}, {"$set": {"data.path": updated_path}}
)
print(f"Modified {result.modified_count} documents.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment