阿帕奇+蟒蛇 + Mongo:对象 ID 不相等
我们正在 Apache 实例中运行基于 django 的应用程序的两个副本。从数据库加载对象时,我们有以下 python 代码:
id = pymongo.objectid.ObjectId(hex_string)
d = self.collection.find_one({ '_id': id })
assert id == d['_id']
在两个应用程序之一(无论我们点击第二个),断言失败。我们查看了 id,它们是相同的。另外,当我们将其更改为:
assert str(id) == str(d['_id'])
断言通过。在我们的开发机器上(Win 7 64 位,django 开发服务器而不是 Apache),这似乎工作正常。
堆栈:Ubuntu 10.04 LTS、Apache 2.2.14、Python 2.6.5、MongoDB 2.0、Pymongo 2.0.1
更新: 我们遇到了另一个这样的问题。我们实际上开始将对象称为 BSON 对象 ID,这解决了第二个问题。但是,即使使用 BSON 对象 ID,此问题中的问题仍然存在。
We are running two copies of a django-based application within an Apache instance. We have this python code when loading an object from the database:
id = pymongo.objectid.ObjectId(hex_string)
d = self.collection.find_one({ '_id': id })
assert id == d['_id']
On one of the two applications (whichever we hit second) the assert fails. We've looked at the ids, and they are the same. Plus, when we change it to:
assert str(id) == str(d['_id'])
The assert passes. On our development machines (Win 7 64-bit, django dev server instead of Apache) this seems to work fine.
Stack: Ubuntu 10.04 LTS, Apache 2.2.14, Python 2.6.5, MongoDB 2.0, Pymongo 2.0.1
Update: We ran into another problem like this. We actually started referring to the objects as BSON object ids, and that fixed the second problem. However, the problem in this question is still occurring, even with using BSON object ids.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点盲目,但你绝对不会使用分片,是吗?我还没有测试过它,但如果您在非 _id 字段上进行分片,似乎有可能有两个具有相同 _id 的文档。您是自己生成 _id 值还是让 pymongo 在插入时为您生成?
Kind of a shot in the dark, but you aren't using sharding by any chance, are you? I haven't tested it, but it seems like there is a possibility of having two docs with the same _id if you shard on a non-_id field. Are you generating the _id values yourself or letting pymongo do it for you on insert?