首页 > 解决方案 > 如何在 mongdb 中获取值

问题描述

这是我在 Mongodb 中的数据

{
    "d" :  {
        "results" :  [
            {
                "slack_id" :  "RAGHU@TN.COM",
                "connector_id" :  "GRECLNT900",
                "sys_role" :  "DEV",
                "user_id" :  "RAGHU"     
            },
            {
                "slack_id" :  "RAGHU@TN.COM",
                "connector_id" :  "GRECLNT900",
                "sys_role" :  "PRD",
                "user_id" :  "RAGHU",
               "question" : "What is your favorite color?",
               "Answer" : "Orange"      
            },         
         ]  
     }
}

如果我给 RAGHU@TN.COM。然后我想显示 sys_role。像这样输出[DEV, PRD]

我正在尝试这种方式

x = mydb.mycollection.distinct("sys-role")

但我得到一个空数组[ ]

标签: mongodbmongodb-querypymongopymongo-3.x

解决方案


您必须将光标视为引用(我个人将其视为 C 中的引用),然后将其取消引用以查看结果。(地址内部是什么)对于特定列,这是命令的结果迅速的:

my_cursor = mydb.mycollection.distinct("sys-role")
for x in my_cursor:
    print('{0}'.format(x['sys_role']))

推荐阅读