Converting MongoDB query to spring

放肆的年华 提交于 2020-04-30 06:26:59

问题


I got a big mongo script and working with spring boot. I need to execute it as either native query or using aggregation framework. I can write lookup,unwind,group in aggregation framework. But I dont know how to write nested $addFields, $map, $reduce , $arrayToObject, $floor, $multiply and $divide.

For example,

{
    $lookup: {} //lookup script
},
{
    $unwind // unwind script
},
{
    $addFields: {
      ans: {
        $reduce: {
          input: {
            $map: {
              input: "$ans",
              in: { $objectToArray: "$$this" }
            }
          },
          initialValue: [],
          in: {
            $concatArrays: [
              "$$value",
              "$$this"
            ]
          }
        }
      }
    }
},
{
    $group : // group
},
{
    $addFields: {
      ans: {
        $map: {
          input: "$ans",
          in: {
            $arrayToObject: [
              [
                {
                  k: "$$this.v",
                  v: "$$this.count"
                },
                {
                  k: "percentage",
                  v: {
                    $floor: {
                      $multiply: [
                        {
                          $divide: [
                            "$$this.count",
                            "$total"
                          ]
                        },
                        100
                      ]
                    }
                  }
                }
              ]
            ]
          }
        }
      }
    }
 }

I =f this is difficult, please let me know how can I write a native query (copy this query and paste in repository or something)?

来源:https://stackoverflow.com/questions/61381113/converting-mongodb-query-to-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!