Created
April 19, 2024 21:54
-
-
Save tbernacchi/df6ff62c5f61094dc54127148d10c7f3 to your computer and use it in GitHub Desktop.
mongodump using python
This file contains 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 datetime import datetime, timedelta | |
from pymongo import MongoClient | |
import bson | |
def get_date_90_days_ago_ms(): | |
# Calculates the timestamp 90 days ago in milliseconds. | |
ninety_days_ago = datetime.now() - timedelta(days=90) | |
# Convert to milliseconds | |
return ninety_days_ago.strftime('%s') | |
timestamp_ms = get_date_90_days_ago_ms() | |
# print(f"Timestamp 90 days ago: {timestamp_ms}") | |
# Connection details | |
connection_string = "mongodb://localhost:27017/" | |
database_name = "pipelinecore" | |
collection_name = "auditing" | |
#query = { "dateStart": {"$gte": timestamp_ms }} # Query to filter documents - Need to double check. | |
output_dir = "/tmp" | |
#Connect to MongoDB | |
client = MongoClient(connection_string) | |
db = client[database_name] | |
# Dump the collection data with query | |
try: | |
with open(f"{output_dir}/auditing-90-days.json", "wb") as dump_file: | |
# Construct the query as a dictionary | |
query = {"dateStart": {"$gte": 1703714258654}} # Greater than or equal to 1703714258654 | |
# Use the query with find() method | |
for doc in db[collection_name].find(query): | |
dump_file.write(bson.BSON.encode(doc)) # Serialize each document to BSON | |
#print(doc) # Uncomment if you want to print each document | |
print(f"Data dumped to {output_dir}/auditing-90-days.json successfully") | |
except Exception as e: | |
print(f"Error during data dump: {e}") | |
finally: | |
# Close the connection | |
client.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment