为何 PyMongo 在记录中插入 _id 字段

来源:互联网 发布:淘宝加盟公司靠谱吗 编辑:程序博客网 时间:2024/06/02 04:10

  • 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:

>>> my_doc = {'x': 1}>>> collection.insert_one(my_doc)<pymongo.results.InsertOneResult object at 0x7f3fc25bd640>>>> my_doc{'x': 1, '_id': ObjectId('560db337fba522189f171720')}

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:

>>> doc = {}>>> collection.insert_many(doc for _ in range(10))Traceback (most recent call last):...pymongo.errors.BulkWriteError: batch op errors occurred>>> doc{'_id': ObjectId('560f171cfba52279f0b0da0c')}>>> docs = [{}]>>> collection.insert_many(docs * 10)Traceback (most recent call last):...pymongo.errors.BulkWriteError: batch op errors occurred>>> docs[{'_id': ObjectId('560f1933fba52279f0b0da0e')}]

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.


0 0
原创粉丝点击