나는 pymongo를 사용하고 있으며 고유하고 희소성이있는 것으로부터 고유 색인을 업데이트하려고합니다. ensure_index
과 create_index
모두 이미 존재하는 색인을 업데이트하려는 것 같지 않습니다. 몽고를 실행하고 있습니다. 2.2.0pymongo에서 기존 색인 업데이트
In [1]: coll.index_information()
Out[1]:
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
u'foo_1': {u'key': [(u'foo', 1)], u'unique': True, u'v': 1}}
In [2]: coll.ensure_index([('foo', 1)], unique=True, sparse=True)
In [3]: coll.index_information()
Out[3]:
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
u'foo_1': {u'key': [(u'foo', 1)], u'unique': True, u'v': 1}}
In [4]: coll.drop_index('foo_1')
In [5]: coll.ensure_index([('foo', 1)], unique=True, sparse=True)
Out[5]: u'foo_1'
In [6]: coll.index_information()
Out[6]:
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
u'foo_1': {u'key': [(u'foo', 1)], u'sparse': True, u'unique': True, u'v': 1}}
아이디어가 있으십니까? 프로덕션 데이터베이스에서 수행 한 작업을 정확히 수행 할 수 없으며 인덱스를 삭제하고 다시 작성합니다.
대안이없고 프로덕션 환경에서 복제본 또는 샤드를 실행중인 경우 http://docs.mongodb.org/manual/tutorial/build-indexes-on-replica-sets/를 사용하여 빌드를 고려할 수 있습니다. #procedure 따라서 보조 색인을 삭제하고 스파 스 사실을 사용하여 동일한 색인을 작성할 수 있습니다. 이 잘 작동합니다. –