unltd_J
unltd_J

Reputation: 494

mongoDB unique, compound index with pymongo

trying to create a compound unique index in mongoDb with PyMongo to avoid duplicates. Here is my code;

index = collection.create_index([('date', 1), ('name', 1)], {'unique' : True})

It is raising the following error.

AttributeError: 'dict' object has no attribute '_pinned_address'

If I change the dict in the argument to a tuple, I get the same error except 'tuple' object. I just went ahead and created the index through mongo compass, but would like to know a solution with PyMongo. Any ideas?

Upvotes: 1

Views: 2099

Answers (1)

Robert Seaman
Robert Seaman

Reputation: 2582

According to the official pymongo documentation: https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.create_index

unique should be a kwarg, therefore, your code should be:

index = collection.create_index([('date', 1), ('name', 1)], unique=True)

Upvotes: 3

Related Questions