问题
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