How to get set of missing consecutive numbers array only has low and high mongodb

无人久伴 提交于 2021-01-29 05:04:29

问题


I am trying to get sets of having an array of missing consecutive numbers only has [low, high]

Example, mongodb collection 'meta_import' has these numbers: [1,2,3,4,7,8,100,101,200,400,401,403]

I want to get [[5,6], [9,99], [102,199], [201,399], [402]]

I have tried:

var pipe = [
    {'$group' : {'_id' : null, min : {'$min' : "$num"}, max : {'$max' : "$num"}}},
    {'$addFields' : {'rangeIds' : {'$range' : ["$min", "$max"]}}},
    {'$lookup' : {'from' : "meta_import", localField : "rangeIds", foreignField : "num", as : "meta_import"}},
    {'$project' : {'_id' :0, missingNums : {'$setDifference' : ["$rangeIds", "$meta_import.num"]}}}
]
meta_import.aggregate(pipe, {'allowDiskUse':true}).toArray(function(err, docs){}

I get all numbers between min and max, like [[5,6], [9,10,11,12.....99], [102,103,...199], [201...399], [402]

It causes overflow mongodb, if missing numbers are too many.

Please help.

来源:https://stackoverflow.com/questions/61921598/how-to-get-set-of-missing-consecutive-numbers-array-only-has-low-and-high-mongod

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