I’ve been experimenting with different ‘method’ values as mentioned in the design documentation. I tested different values for the method parameter in the $group stage using the following code snippet:
test.aggregate( [
{
$group: {
_id: "$t",
Percentile: {
$percentile: {
p: [
0.9
],
method: "discrete",
input: "$a"
}
}
}
}
])
For both ‘continuous’ and ‘discrete’ values, I received the error message{} 'MongoServerError: Currently only approximate percentiles are supported'.
As I understand it, this aligns with the comment in the design documentation provided here “https://docs.google.com/document/d/1NVQ6hiD3rvt03Eegb9JZj5mtWMHnX1ueumAbejwdOWY/edit?disco=AAAAvPx8do4”. It seems that only ‘approximate’ is supported as of now.
However, I noticed that when I used the $project stage with 'continuous', 'discrete' and 'approximate' values, they all worked fine if I used a field reference in the input parameter. Here’s an example:
test.aggregate([
{
$project: {
percentile: {
$percentile: {
input: $a,
p: [
0.5,
0.9,
0.95
],
method: "continuous"
}
}
}
}
])
The same with $setWindowFields stage, it supports all three methods without any issues. For example:
test.aggregate( {
$setWindowFields: {
partitionBy: "$t",
sortBy: {
t: 1
},
output: {
sat_p95: {
$percentile: {
input: "$a",
p: [
0.95
],
method: "continuous"
},
window: {
documents: [
-1,
0
]
}
}
}
}
})
Collection test data:
[
{ _id: ObjectId("6477bc4707718501f4056fab"), t: 0, a: 1 },
{ _id: ObjectId("6477bc4707718501f4056fac"), t: 0, a: 2 },
{ _id: ObjectId("6477bc4707718501f4056fad"), t: 1, a: 2 },
{ _id: ObjectId("6477bc4707718501f4056fae"), t: 1, a: 4 },
{ _id: ObjectId("6477bc4707718501f4056faf"), t: 1, a: 5 },
{ _id: ObjectId("6477bc4707718501f4056fb0"), t: 1, a: 6 },
{ _id: ObjectId("6477bc4707718501f4056fb1"), t: 1, a: 4 },
{ _id: ObjectId("6477bc4707718501f4056fb2"), t: 1, a: 1 },
{ _id: ObjectId("6477bc4707718501f4056fb3"), t: 1, a: 2 },
{ _id: ObjectId("6477bc4707718501f4056fb4"), t: 1, a: 3 },
{ _id: ObjectId("6477bc4707718501f4056fb5"), t: 1, a: 1 },
{ _id: ObjectId("6477bc4707718501f4056fb6"), t: 1, a: 100 }
]