我正在用python编写脚本,该脚本从MongoDB集合中获取索引信息,这里是我的脚本
from pymongo import MongoClient
from pprint import pprint
import dbconfig
import sys
import os
prod = MongoClient(dbconfig.prod_url)
result_index = prod['testcms']['assess'].index_information()
['assessment'].index_information()
print("index count is, ",result_index)
我得到的结果是这个
('index count is, ', {u'submitted_time_1': {u'background': True, u'key': [(u'submitted_time', 1.0)], u'v': 2, u'ns': u'testcms.assess', u'sparse': True}, u'attempt_id_hashed': {u'ns': u'testcms.assess', u'key': [(u'attempt_id', 1)], u'v': 2, u'unique': True, u'background': True}, u'_id_': {u'ns': u'testcms.assess', u'key': [(u'_id', 1)], u'v': 2}, u'worksheet_id_1': {u'key': [(u'worksheet_id', 1)], u'ns': u'testcms.assess', u'background': True, u'v': 2}, u'submitter_id_hashed': {u'ns': u'testcms.assess', u'key': [(u'submitter_id', u'hashed')], u'v': 2}})
现在我只想从整个结果中显示/获取特定值,而这些值就是
1。 Submitter_id_hashed
2。 Submitted_time_1
3。 try_id_hashed
4。 worksheet_id_1
如何仅在输出中显示以下值?
答案 0 :(得分:0)
只需使用键访问它们
result_index["submitter_id_hashed"]
result_index["submitted_time_1"]
result_index["attempt_id_hashed"]
result_index["worksheet_id_1"]
答案 1 :(得分:0)
首先,声明您想要的密钥的列表:
list = ["submitter_id_hashed", "submitted_time_1", "attempt_id_hashed", "worksheet_id_1"]
使用此:
result = {key: result[key] for key in list}