using $slice array aggregation operator and then replace it with sum of all elements it contains (stages 4-5){ "_id" : 1, "c" : 2 } -> csum = 2{ "_id" : 2, "c" : 6 } -> csum = 8{ "_id" : 3, "c" : 1 } -> csum = 9使用 $unwind 阶段和 $sum 组累加器运算符.然后用 csum >= C (stage 6) 构造另一个文档辅助数组Then construct another helper array of documents with csum >= C (stage 6)/* Ex. (C = 8) */gteC = [ { "_id" : 3, "c" : 1, "csum" : 9 }, { "_id" : 2, "c" : 6, "csum" : 8 } ]最后一步是检索所有带有 csum 的文档.这是使用 $filter 数组聚合运算符(阶段 7).The last step is to retrieve all documents with csum <= Min { gteC.csum }. This is done using $filter array aggregation operator (stage 7).但是,我不确定确定这是实现您想要的最有效的聚合管道(将感谢任何改进建议).However, I am not sure this is the most efficient aggregation pipeline (will be grateful for any improvement suggestions) to achieve what you want.PS 在测试查询之前不要忘记更改集合的名称并替换 SET_DESIRED_VALUE_FOR_C_HERE.PS Before testing the query don't forget to change the name of collection and replace SET_DESIRED_VALUE_FOR_C_HERE. 这篇关于mongodb:查询特定列的总和大于或等于 C 的前几行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 09:13