Murilo Sitonio
Murilo Sitonio

Reputation: 305

Why pymongo change my variable after insert_one operation?

I faced this while writing some tests to a new class method.

>>> doc = {'test': True}
>>> collection.insert_one(doc)
<pymongo.results.InsertOneResult object at 0x031C2D00>
>>> doc
{'test': True, '_id': ObjectId('5e7103a1b650bd01fac6c6ff')}

Why the key _id was added to the variable doc? There's a way to avoid this behaviour from pymongo?

Upvotes: 2

Views: 577

Answers (1)

kederrac
kederrac

Reputation: 17322

from the docs:

insert_one(document, bypass_document_validation=False, session=None)

(...)

Parameters: document: The document to insert. Must be a mutable mapping type. If the document does not have an _id field one will be added automatically.

Upvotes: 3

Related Questions