V
V
VitaliySm2015-02-16 17:17:37
Python
VitaliySm, 2015-02-16 17:17:37

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

3 answer(s)
V
VitaliySm, 2015-02-17
@VitaliySm

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)])

A
Arman, 2015-02-16
@Arik

It is necessary to put a unique index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question