【发布时间】:2021-01-03 08:07:28
【问题描述】:
我有 MongoDB 集合,其中包含许多文档,每个文档的字段看起来像图片中显示的那样。问题在于“搜索”字段。它的值存储为字符串,因此我无法查询像 {"searched.image_hash":"some_value"} 这样的值。我使用 python 将值存储到 MongoDB 中。在python中,在mongo中存储为“searched”的变量“to_search”实际上是一个字典。我不确定为什么“to_search”变量中的字典作为字符串存储在 mongodb“搜索”字段中。关于如何将字典作为对象数组存储在 mongodb 中的任何建议?
我在python中使用的代码如下
i have many other keys going into dictionary 'di'
di['account_id'] = acc_num
di['searched']= to_search
di['breakdown_queried'] = breakdown_to_query
di['combination']= [ele for ele in to_search.keys()]
di['ad_ids'] = ad_ids
di['date'] = date.today()
lo_str= ''
di = {k: str(v) for k, v in di.items()}
mongo_obj_remote.client["dev"]["ad_stats_tracker"].delete_one({"_id": {"$in": [di['_id']]}})
di_key_li = ['_id','account_id','date', 'combination','searched', 'ad_ids','breakdown_queried']
mongo_obj_remote.insert_single_document("dev", "ad_stats_tracker", {key: di[key] for key in di_key_li})
【问题讨论】:
-
你能显示保存
searched字段的代码吗? -
@balderman 嗨,我的问题完全不同,我很困惑为什么字典存储为字符串而不是对象。我的意图是将它存储为对象,以便我可以进行这样的查询 {"searched.image_hash":"some_value"}