- MongoDB 要求每个文档都必须有 _id
- 如果待插入的文档没有 _id, MongoDB 会自动生成一个,但不会把结果返回个 PyMongo
- 对于需要写操作频繁的应用来说, 在写入之前复制一份插入 _id 代价可能会很高
如果你不想自动生成的 _id, 就需要自己在插入数据前 自己准备好 _id 字段
Why does PyMongo add an _id field to all of my documents?¶
When a document is inserted to MongoDB using insert_one()
, insert_many()
, or bulk_write()
, and that document does not include an _id
field, PyMongo automatically adds one for you, set to an instance of ObjectId
. For example:
Users often discover this behavior when calling insert_many()
with a list of references to a single document raises BulkWriteError
. Several Python idioms lead to this pitfall:
PyMongo adds an _id
field in this manner for a few reasons:
- All MongoDB documents are required to have an
_id
field. - If PyMongo were to insert a document without an
_id
MongoDB would add one itself, but it would not report the value back to PyMongo. - Copying the document to insert before adding the
_id
field would be prohibitively expensive for most high write volume applications.
If you don’t want PyMongo to add an _id
to your documents, insert only documents that already have an _id
field, added by your application.