返回一个python查询的大小,单位为字节。

问题描述 投票:0回答:1

我有以下pymongo代码。

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))

我怎样才能知道这个查询执行后的大小?

python mongodb pymongo
1个回答
1
投票

如果你检查你的字符串表示的长度,你的 query 列表中,你会得到列表中的字节数。

试试吧。

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

你可以把它写到磁盘上,然后在你的文件查看器中检查它的大小(当然,写一个列表的字符串表示和列表的字符串表示的长度应该是一样的)。

这么简单的事情。

with open('<path to and file name>', 'w') as f:
    f.write(str(query))
© www.soinside.com 2019 - 2024. All rights reserved.