JediJesus
JediJesus

Reputation: 329

Return the size of a python query in bytes

I have the following pymongo code:

import pymongo
import time
start_time = time.time()

connection_string = 'mongodb://localhost'
connection = pymongo.MongoClient(connection_string)
database = conn
pipe = [ 
  *some code here*
  ]
start_time = time.time()
query = list(database.solution3.aggregate(pipe,allowDiskUse=True))
print("--- %s seconds ---" % (time.time() - start_time))

How can i know the size of this query after it's executed??

Upvotes: 0

Views: 490

Answers (1)

DaveStSomeWhere
DaveStSomeWhere

Reputation: 2540

If you check the length of the string representation of your query list you will get the number of bytes in the list.

Just try:

print(f'Length/size of query is --> {len(str(query))}')

You can verify by writing it to disk and checking the size in your file viewer (of course writing a string representation of a list and the length of a string representation of a list should be the same).

Something as simple as:

with open('<path to and file name>', 'w') as f:
    f.write(str(query))

Upvotes: 1

Related Questions