Jack
Jack

Reputation: 413

Using dictionary key as the _id when using pymongo

I have a dictionary like so:

a_dictionary = {
                'UniqueNumber': 1,
                'YearResults' : [],
               }

When I use:

MyCollection.replace_one(
        {'UniqueNumber': a_dictionary['UniqueNumber']}, a_dictionary, upsert=True)

Mongodb is creating an _id field: _id: 5e467caa3cec0556ddcb3a41.

I don't want this, I want to use my UniqueNumber and have no _id field.

Upvotes: 1

Views: 291

Answers (1)

N.Moudgil
N.Moudgil

Reputation: 869

As per the documentation: Insert document

When a document is inserted a special key, "_id", is automatically added if the document doesn’t already contain an "_id" key. The value of "_id" must be unique across the collection

So in order to have your custom identifier you can add _id to the record you are inserting. Please note The value of "_id" must be unique across the collection

Hope it helps.

Upvotes: 1

Related Questions