我得到了一个我实际上并没有尝试的解决方案,但我认为我们可以使用它来管理。`
数据类型的默认清单
key_type_default_count = {
int: 0,
float: 0,
str: 0,
bool: 0,
dict: 0,
list: 0,
set: 0,
tuple: 0,
None: 0,
object: 0,
unicode: 0,
"other": 0,
}
获取mongo连接的自定义代码
client = create_mongo_con(v_env,v_con_name)
print client
db = client[v_db_name]
collection = db[v_collection]
主要代码
key_type_count = defaultdict(lambda: dict(key_type_default_count))
mongo_collection_docs = collection.find({},{"_id":0}).limit(30)
#print mongo_collection_docs'
print type(mongo_collection_docs)
for doc in mongo_collection_docs:
for key, value in doc.items():
print ' my key '+str(key)
print 'my value is '+str(value)
print ' my value type '
print type(value)
if type(value) in key_type_count[key].keys():
key_type_count[key][type(value)] += 1
else:
key_type_count[key]["other"] += 1
total_docs += 1`
您可以参考更多关于 https://github.com/nimeshkverma/mongo_schema 的信息,我从那里得到了这个想法,但该代码不起作用。我编辑了一些部分,现在我能够生成这样的漂亮输出
但现在我面临一个问题,所有字符串字段都被检测为 unicode。如果我们有解决方案,我需要弄清楚这一点。如果有人在 python 中遇到 str 和 unicode 相同的问题,请发表评论