Answer the question
In order to leave comments, you need to log in
How to correctly set a unique field in mongodb?
When adding information to the collection, I want to make p_id unique I tried
to do this:
self.prod_coll.insert({ 'p_id': item['p_id'], 'unique': True, code': item['code'], 'page_url ': item['page_url']})
but it didn't work. where is the mistake?
Answer the question
In order to leave comments, you need to log in
Solved the issue like this:
self.prod_coll.insert({ 'p_id': item['p_id'], 'code': item['code'], 'page_url': item['page_url']})
self.prod_coll.ensure_index ([('p_id', pymongo.ASCENDING), ('unique', True)])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question