If you can accept getting all individual IDs back that are missing as opposed to ranges then this is your query:
collection.aggregate({
$group: {
"_id": null, // group all documents into the same bucket
"numbers":
{
$push: "$number" // create an array of all "number" fields
}
}
}, {
$project: {
"_id": 0, // get rid of the "_id" field - not really needed
"numbers": {
$setDifference: [ { // compute the difference between...
$range: [ 0, 10 ] // ... all numbers from 0 to 10 - adjust this to your needs...
}, "$numbers" ] // ...and the available values for "number"
}
}
})
There are ways to compute the ranges out of this information but I have a feeling this might not even be needed in your case.
UPDATE (based on your comment): Here is a longer version that adds some additional stages to get from the discrete numbers to to the ranges - the code is not exactly pretty and probably not super fast but it should be working at least...
collection.aggregate({
$sort: {
"number": 1 // we need to sort in order to find ranges later
}
},
{
$group: {
"_id": null, // group all documents into the same bucket
"numbers":
{
$push: "$number" // create an array of all "number" fields
}
}
}, {
$project: {
"_id": 0, // get rid of the "_id" field - not really needed
"numbers": {
$setDifference: [ { // compute the difference between...
$range: [ 0, 10 ] // ... all numbers from 0 to 10 - adjust this to your needs...
}, "$numbers" ] // ...and the available values for "number"
}
}
},
{
$project: {
"numbers": "$numbers", // ...we create two identical arrays
"numbers2": "$numbers" // ...by duplicating our missing numbers array
}
},
{
$unwind: "$numbers" // this will flatten one of the two created number arrays
},
{
$project: {
"number": "$numbers",
"precedingNumber": {
$arrayElemAt: [
"$numbers2", // use the second (remaining) numbers array to find the previous number...
{ $max: [0, { $add: [ { $indexOfArray: [ "$numbers2", "$numbers" ] }, -1 ] } ] } // ...which needs to sit in that sorted array at the position of the element we're looking at right now - 1
]
},
"followingNumber": {
$arrayElemAt: [
"$numbers2", // use the second (remaining) numbers array to find the next number...
{ $add: [ { $indexOfArray: [ "$numbers2", "$numbers" ] }, 1 ] } // ...which needs to sit in that sorted array at the position of the element we're looking at right now + 1
]
}
}
}, {
$project: {
"number": 1, // include number
"precedingInRange": { $cond: [ { $eq: [ { $add: [ "$number", -1 ] }, "$precedingNumber" ] }, true, false ] },
"followingInRange": { $cond: [ { $eq: [ { $add: [ "$number", 1 ] }, "$followingNumber" ] }, true, false ] }
}
}, {
$match: {
$or: [ // filter out all items that are inside a range (or rather: include only the outer items of each range)
{ "precedingInRange": false },
{ "followingInRange": false }
]
}
}, {
$project: { // some beautification of the ouput to help deal with the data in your application
"singleNumber": { $cond: [ { $not: { $or: [ "$precedingInRange", "$followingInRange" ] } }, "$number", null ] },
"startOfRange": { $cond: [ "$followingInRange", "$number", null ] },
"endOfRange": { $cond: [ "$precedingInRange", "$number", null ] }
}
})
UPDATE 2:
I have a feeling I found a way better way to nicely get the ranges without too much magic involved:
collection.aggregate({
$sort: {
"number": 1 // we need to sort by numbers in order to be able to do the range magic later
}
}, {
$group: {
"_id": null, // group all documents into the same bucket
"numbers":
{
$push: "$number" // create an array of all "number" fields
}
}
}, {
$project: {
"numbers": {
$reduce: {
input: "$numbers",
initialValue: [],
in: {
"start": {
$concatArrays: [
"$$value.start",
{
$cond: { // if preceding element in array of numbers is not "current element - 1" then add it, otherwise skip
if: { $ne: [ { $add: [ "$$this", -1 ] }, { $arrayElemAt: [ "$numbers", { $add: [ { $indexOfArray: [ "$numbers", "$$this" ] }, -1 ] } ] } ] },
then: [ "$$this" ],
else: []
}
}
]
},
"end": {
$concatArrays: [
"$$value.end",
{
$cond: { // if following element in array of numbers is not "current element + 1" then add it, otherwise skip
if: { $ne: [ { $add: [ "$$this", 1 ] }, { $arrayElemAt: [ "$numbers", { $add: [ { $indexOfArray: [ "$numbers", "$$this" ] }, 1 ] } ] } ] },
then: [ "$$this" ],
else: []
}
}
]
}
}
}
}
}
}, {
$project: {
"ranges": {
$zip: {
inputs: [ "$numbers.start", "$numbers.end" ],
}
}
}
})