duanquan1243 2019-03-05 09:32
浏览 298
已采纳

如何将数组中存在的值与mongo中的字段进行匹配

I have this data set in mongo

{
  _id: {
    question_id: 3
  },
  data: {
    answer: 11,
    count: 114,
    result: [
      {
        _id: 3,
        question: "What is your age group?",
        options: [
          {
            id: 10,
            option: "<18"
          },
          {
            id: 11,
            option: "18-24"
          },
          {
            id: 12,
            option: "24-35"
          },
          {
            id: 13,
            option: ">35"
          }
        ],
        type: "Radio"
      }
    ]
  }
},

Here i would like to perform an equals to check between answer and result.options.id and if the value matches it should return result.options.option

So that the desired result is

{
  _id: {
    question_id: 3
  },
  data: {
    answer:'18-24',
    count: 114,
    
  }
},

So far i have tried this

'$project'=>[
  'res'=>[
    '$map'=>[
      'input'=>'$data.result.options',
      'as'=>'dt',
      'in'=>[
        '$cond'=>[
          'if'=>[
            '$eq'=>[
              '$$dt.id',
              '$data.answer'
            ]
          ],
          'then'=>'$$dt.option',
          'else'=>'$$dt.id'
        ]
      ]
    ]
  ]
]

Any clue as to what i am doing wrong . My code always returns the else statement

</div>
  • 写回答

1条回答 默认 最新

  • duanli0119 2019-03-05 10:00
    关注

    You were close, but there are a couple of things not quite right:

    [ '$project'=> [
      'data' => [
        'answer' => [
          '$let' => [
            'vars' => [
              'res' => [
                '$reduce' => [
                  'input' => '$data.result.options',
                  'initialValue' => [],
                  'in' => [ '$concatArrays': [ '$$value', '$$this' ] ]
                ]
              ]
            ],
            'in' => [
              '$arrayElemAt' => [
                '$$res.option',
                [ '$indexOfArray': [ '$$res.id', '$data.answer' ] ]
              ]
            ]
          ]
        ],
        'count' => '$data.count'
      ]
    ]]
    

    The path to '$data.result.options' is actually within nested arrays, so in order for any "array operations" to work in the inner value of the 'options' you need to "flatten" that array structure into a singular array.

    In order to do that you would use $reduce along with $concatArrays. This is also demonstrated in a $let block, since you want to use this "result array" in some later operations.

    That being the most simple way to extract a single value is to find the matching index of the array via $indexOfArray to 'data.answer' and then extract the 'option' value from your "result array" processed earlier for that value using $arrayElemAt of course pointing at that matched "index".

    The $indexOfArray operator is reliable here as long as you will always have an answer matching the 'data.answer' value present within the document array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站