我正在使用 Python 3.4 解析从 MongoDB (3.0.2) 读取的文档 - 我执行各种测试并生成以下形式的 JSON/BSON:
{
'FixedH': False,
'Mstereo': True,
'RecMet': False,
'Sstereo': True,
'bond': False,
'charge': False,
'isotope': False,
'length': 223,
'nocomponents': 1,
'nolayers': 6,
'stereo': True
}
如果我尝试将其写回 MongoDB(从 shell),我会收到以下错误:
ReferenceError: False is not defined at (shell):1:175
如果我手动转换我的 bool 值 (False --> false) 以便它们全部为小写,则错误消失并且文档被写入 MongoDB 中的集合。
我猜我不是第一个遇到这个问题的人,但我找不到任何已发布的解决方法。我怎样才能解决这种大小写不匹配的问题?
最佳答案
您是从 mongo shell 插入文档吗? Mongo shell 不接受“False”或“True”作为 bool 值。您应该使用 Python Mongo 驱动程序客户端。 Pymongo 应该工作。检查以下示例:
import pymongo
client = pymongo.MongoClient('localhost', 27017)
db = client.testdatabase
col = db.testcollection
col.insert({'FixedH': False,'Mstereo': True,'RecMet': False,'Sstereo': True,'bond': False,
'charge': False, 'isotope': False,'length': 223,'nocomponents': 1,
'nolayers': 6,'stereo': True})
cursor = col.find()
print 'Found', cursor.count()
print cursor.next()
client.close()
关于python - 如何将在 Python 中创建的 bool 值传递给 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286401/