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