1

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.

user3848207
  • 3,737
  • 17
  • 59
  • 104
  • Generate an UUID for each record, https://stackoverflow.com/questions/534839/how-to-create-a-guid-uuid-in-python or are you looking for a running sequence ? – sushanth Jun 06 '20 at 05:24
  • @Sushanth, No need running sequence. As long as the field can be kept unique like in a relational database, that will be good enough. – user3848207 Jun 06 '20 at 06:48
  • Then you can go with UUID – sushanth Jun 06 '20 at 06:49

2 Answers2

2

Before inserting, You would need to create an index in MongoDB and mark CODE field as a unique index

creating index in mongo

db.mongo_collection.createIndex({CODE:1}, {unique:true})

This would ensure you get a duplicate key error if you try to insert CODE that already exists.

If you don't have access to MongoDB server.

You would need to populated _id with value as same as CODE. Since _id is a default primary index in mongo.

json_data = [
  {
    "_id": "018906",
    "CODE": "018906",
    "X": "29813.66349",
    "Y": "28697.520760000003"
  },
  {
    "_id": "018907",
    "CODE": "018907",
    "X": "30041.8389",
    "Y": "28602.98724"
  },
  {
    "_id": "018910",
    "CODE": "018910",
    "X": "31966.120789999997",
    "Y": "29115.75337"
  },
]
Anurag Wagh
  • 1,086
  • 6
  • 16
  • Thank you for the answer. How do you determine `{CODE:1}`? Why 1 and not some other number? – user3848207 Jun 06 '20 at 07:46
  • 1
    `{field:1}` here '1' indicates the creation of ascending index i.e. when indexes are created they will be sorted in ascending order. https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#examples – Anurag Wagh Jun 06 '20 at 07:49
1

I would like to answer my own question to build on Anurag Wagh's answer as the syntax he used is not python. I have marked his answer as the correct one.

Since I am using pymongo, the syntax of the code should be;

import pymongo
mongo_collection.create_index([("CODE", pymongo.ASCENDING)], unique=True)
user3848207
  • 3,737
  • 17
  • 59
  • 104