PyMongo create unique index with 2 or more fields PyMongo create unique index with 2 or more fields python python

PyMongo create unique index with 2 or more fields


You need to create a compound index and set unique to True as mentioned in the documentation:

If you use the unique constraint on a compound index, then MongoDB will enforce uniqueness on the combination of values rather than the individual value for any or all values of the key.

self.db[self.mongo_collection].create_index(    [("url", pymongo.DESCENDING), ("category", pymongo.ASCENDING)],    unique=True)