服务器在pymongo上生成的ObjectID [英] ObjectID generated by server on pymongo
问题描述
我正在使用pymongo(用于mongodb的python模块).
I am using pymongo (python module for mongodb).
我希望由服务器自动创建ObjectID,但是当我们不指定它时,它似乎是由pymongo本身创建的.
I want the ObjectID to be created automatically by the server, however it seems to be created by pymongo itself when we don't specify it.
它引起的问题是我使用ObjectID按时间排序(仅按_id字段排序).但是,它似乎正在使用每台计算机上设置的时间,因此我们不能真正依靠它. 关于如何解决此问题的任何想法吗?
The problem it raises is that I use ObjectID to sort by time (by just sorting by the _id field). However it seems that it is using the time set on each computer so we cannot truly rely on it. Any idea on how to solve this problem?
推荐答案
If you call save
and pass it a document without an _id
field, you can force the server to add the _id
instead of the client by setting the (enigmatically-named) manipulate
option to False
:
coll.save({'foo': 'bar'}, manipulate=False)
这篇关于服务器在pymongo上生成的ObjectID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!