【问题标题】:Mongodb query to return field valueMongodb查询返回字段值
【发布时间】:2021-01-10 16:52:31
【问题描述】:

我正在尝试构建一个 Mongodb 查询来返回一个字段值。我的 JSON 如下所示:

"question" : "Global_Deployment",
                    "displayOrder" : 1,
                    "answerOptions" : {
                        "fieldId" : "1001",
                        "fieldType" : "radiobutton",
                        "fieldName" : "Global Deployment?",
                        "fieldLabel" : "Global Deployment?",
                        "helpText" : "Help will go here",
                        "emailTagFormControl" : "Global_Deployment?",
                        "source" : "custom",
                        "status" : "active",
                        "required" : "true",
                        "multiSelect" : "false",
                        "purgeFlag" : "false",
                        "enableAuditTrack" : "false",
                        "fields" : [],
                        "fieldValue" : "Yes",
                        "options" : [ 
                            {
                                "optionName" : "Yes"
                            }, 
                            {
                                "optionName" : "No"
                            }
                        ],
                        "comments" : {
                            "commentId" : "C1001",
                            "commentDetails" : []
                        }

我查询到的字段名称为“全球部署”的字段是这样的:

db.getCollection('requests').find({"sections.questions.answerOptions.fieldName":"Global Deployment?"})

我想知道要添加到此查询中以返回“fieldValue”的值,该值位于 JSON 中的不同行。我是 MongoDB 的新手。任何帮助将不胜感激。

【问题讨论】:

    标签: mongodb robo3t


    【解决方案1】:

    1) 如果您在 DB 中有多个带有 "fieldName" : "Global Deployment?" 的文档,那么 .find() 将返回所有匹配的文档,即;在输出中你得到的是一个文档数组,那么你需要遍历数组以获得每个文档的answerOptions.fieldValue,检查下面的场景,正如我已经解释的那样,如果"sections.questions.answerOptions.fieldName" 是,就有可能获得多个文档不是唯一字段。

    db.getCollection('requests').find({"sections.questions.answerOptions.fieldName":"Global Deployment?"}, {'sections.questions.answerOptions.fieldValue':1})
    

    查找的输出:

    /* 1 */
    [{
        "_id" : ObjectId("5d4e19826e173840500f5674"),
        "answerOptions" : {
            "fieldValue" : "Yes"
        }
    },
    /* 2 */
    {
        "_id" : ObjectId("5d4e19826e073840500f5674"),
        "answerOptions" : {}
    }]
    

    如果您只需要其中包含 fieldValue 的文档,请执行以下操作:

    db.getCollection('requests').find({"sections.questions.answerOptions.fieldName":"Global Deployment?", 'sections.questions.answerOptions.fieldValue':{$exists: true}}, {'answerOptions.fieldValue':1})
    

    好的,现在您已经有了一系列文档,然后遍历每个文档以检索您的值,检查此mongoDB cursor tutorial

    2) 如果您认为 fieldName 在整个集合中是唯一的,那么您可以使用 .findOne() ,它只会返回一个文档(如果您ve 多个匹配的文档,它将返回第一个找到的文档):

    db.getCollection('requests').findOne({"sections.questions.answerOptions.fieldName":"Global Deployment?"}, {'sections.questions.answerOptions.fieldValue':1})
    

    findOne 的输出:

    {
        "_id" : ObjectId("5d4e19826e173840500f5674"),
        "answerOptions" : {
            "fieldValue" : "Yes"
        }
    }
    

    如果您看到.find({},{}) 有两个参数,第二个参数称为 projection,如果您只想检索响应中的必填字段,它实际上很有用,默认情况下 mongoDB 将返回整个文档 将检索您在问题中发布的任何内容,mongoDB 中的数据以 JSON 的形式流动,因此操作类似于使用 JSON 的,在这里您可以从结果中检索所需的字段,但为了最好地使用如果您不需要整个文档,则网络效率将只使用投影获得所需的字段。

    【讨论】:

      【解决方案2】:

      您可以指定用逗号分隔的第二个条件。您正在尝试使用 $and$or 过滤数据

      用简单的方法:

      {"sections.questions.answerOptions.fieldName":"Global Deployment?","sections.questions.answerOptions.fieldValue":"Yes" }
      

      通过使用 $and 方法:

      .find(
           {
            $and: [
                  {"sections.questions.answerOptions.fieldName":"Global Deployment?"},
                  {"sections.questions.answerOptions.fieldValue":"Yes"}
                  ]
           }
          )
      

      同样可以使用$or 方法。只需将$and 替换为$or

      编辑: 如果您想检索特定值(在您的情况下为 fieldValue),查询将是:

      db.getCollection('requests').find({
      "sections.questions.answerOptions.fieldName":"Global Deployment?"
      }).map(function(item){
      return item.fieldValue
      }) 
      

      【讨论】:

      • 感谢您的回答。如何检索字段值?我不会总是知道它是什么。
      • 您能否指定您提到的 JSON 的预期输出?
      • 在这种情况下我希望查询返回“是”,即 fieldName "Global Deployment" 的 fieldValue
      • 您想在 MongoDb 查询中检索值吗?如果这是您想要的,您可以像 db.getCollection('requests').find(filter query as i mentioned in my answer).forEach(function(item){ item.fieldValue}) 一样查询
      • 我认为您只需要根据fieldName 检索值。在这种情况下,您不需要任何 $or$and 方法
      【解决方案3】:

      这里的正确答案是方法.distinct()(docs)

      在你的情况下试试这样:

      db.getCollection('requests').find({"sections.questions.answerOptions.fieldName":"Global Deployment?"}).distinct('fieldValue');
      

      这将只返回你想要的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 1970-01-01
        相关资源
        最近更新 更多