python - With PyMongo how do I import indexes using create_index() that were exported with index_information()? -
what need output of index_information() such can re-imported using create_index() or create_indexes() ?
>>> pymongo import mongoclient >>> client = mongoclient("mongodb://host1") >>> db = client.mydb >>> collection = db.mycollection >>> index = db.provisioning.index_information() >>> index {u'_id_': {u'ns': u'mydb.mycollection', u'key': [(u'_id', 1)], u'v': 1}, u'name_1': {u'unique': true, u'key': [(u'name', 1)], u'v': 1, u'ns': u'mydb.mycollection', u'background': false}, u'mongotype_1': {u'key': [(u'mongotype', 1)], u'ns': u'mydb.mycollection', u'background': false, u'v': 1}} >>> client2 = mongoclient("mongodb://host2") >>> db2 = client2.mydb >>> collection2 = db2.mycollection >>> collection2.create_index(index) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 1161, in create_index keys = helpers._index_list(keys) file "/usr/lib64/python2.6/site-packages/pymongo/helpers.py", line 55, in _index_list raise typeerror("if no direction specified, " typeerror: if no direction specified, key_or_list must instance of list >>> collection2.create_indexes(index) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 1046, in create_indexes raise typeerror("indexes must list") typeerror: indexes must list
try basic code, iterate , add index 1 collection other collection,
from pymongo import mongoclient client = mongoclient("mongodb://host1") db = client.mydb collection = db.mycollection index = db.provisioning.index_information() client2 = mongoclient("mongodb://host2") db2 = client2.mydb collection2 = db2.mycollection in index.keys(): name_index = index[i]['key'][0][0] order = index[i]['key'][0][1] collection2.create_index([(name_index, order)])
Comments
Post a Comment