服务器在pymongo上生成的ObjectID [英] ObjectID generated by server on pymongo

查看:147
本文介绍了服务器在pymongo上生成的ObjectID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆