I have a list of dictionaries that look something like this;
json_data = [
{
"CODE": "018906",
"X": "29813.66349",
"Y": "28697.520760000003"
},
{
"CODE": "018907",
"X": "30041.8389",
"Y": "28602.98724"
},
{
"CODE": "018910",
"X": "31966.120789999997",
"Y": "29115.75337"
},
]
I tried to insert json_data
into a mongodb collection mongo_collection
.
mongo_collection.insert_many(json_data)
It ran successfully. However, I want the key field CODE
to be unique and insert should fail if there is a duplicate. The line above will insert every document even if there is duplicate CODE
. How can I make CODE
key unique? I am open to using python libraries like mongoengine.
I would like CODE
to be something like a primary key in a relational database.
I am using python 3.7, mongodb 4.2.7 and pymongo.