【发布时间】:2016-08-18 13:16:02
【问题描述】:
我在 getJSON 请求上收到 500 错误,该请求映射到包含 MongoDB update 和 $pull 的 Python 2.7 函数。
sudo tail -f /var/log/apache2/error.log 出现的最后一个错误是:
[wsgi:error] [pid 1721:tid 140612911712000]
[client 127.0.0.1:59078]
KeyError: 'nModified', referer: http://localhost/control_room
处理这个特定键的 Python 逻辑是 update 和 $pull:
update_with_pull = collection.update({"user_email":user_email,"top_level.year":entry_year,"top_level.month":entry_month}, { "$pull": {dynamic_nested_key: {"timestamp":entry_timestamp}}})
这之后的条件是试图捕捉一个文档是否被修改过:
if update_with_pull['nModified'] == 1:
#do stuff
我在 mongo shell 中测试了相同的操作并返回:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
所以它似乎与查询匹配,但没有进行任何修改。
为了进一步排除故障,上面$pull 部分中使用的entry_timestamp 值在数据库和函数中是一致的(即它们都是具有相同字符的字符串)。
一些想法:
- 我刚刚将应用程序从
2.6.11移动到3.2.5MongoDB 环境,这可能会导致问题,但是,如果是这样,我还不能确定是怎么回事。 -
$pull的语法在3.2.5中发生了变化? - 集合存在权限问题,阻止修改?
- 我在
virtualenv中运行pymongo 2.6.2- 也许它与 MongoDB3.2.5存在一些不兼容?
【问题讨论】:
标签: mongodb python-2.7 pymongo ubuntu-16.04